I have a tab control that has listboxes on it some of which are created and named dynamically so I can't statically program their name. Is there a way to create an array of all the list box names on a give tabPage? I have been going nuts trying to figure out a way to do it.
1 Answer
it would look something like this (based on a winforms example)
List<string> listBoxNames = new List<string>();
foreach (Control control in tabPage1.Controls)
{
if (control.GetType() == typeof(ListBox))
{
listBoxNames.Add(control.Name);
}
}
Or the same thing in linq syntax
List<string> listBoxNames = (from Control control in tabPage1.Controls
where control.GetType() == typeof (ListBox)
select control.Name).ToList();
if you want to find all the listbox's in the tabpage again then see below
foreach (var listBoxName in listBoxNames)
{
ListBox listBox = (ListBox) tabPage1.Controls.Find(listBoxName, true)[0];
}
4 Comments
Darw1n34
This seems promising, I am using C# winforms in VS2013. I will check it out and let you know.
Ricky Gummadi
did the solution help?
Darw1n34
The new issue is calling the listbox name as a list box s oI can refer to the items held within. meh..problems beget problems
Ricky Gummadi
thats simple, you just need to iterate through the listbox names and find, I've updated the answer above
Dictionary<TabPage, List<ListBox>>and add them to the dictionary when you add them to a tab page.