1

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.

3
  • you mean you want to fetch all the listbox names in a tab control? is this winforms, wpf? Commented Jun 25, 2015 at 1:30
  • @ricky Yes. I a trying to grab the dynamically created names, so I can't statically code them. Commented Jun 25, 2015 at 1:52
  • By far the simplest and most reliable approach is to not lose it in the first place. You can store them in, say, a Dictionary<TabPage, List<ListBox>> and add them to the dictionary when you add them to a tab page. Commented Jun 25, 2015 at 2:08

1 Answer 1

1

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];
            }
Sign up to request clarification or add additional context in comments.

4 Comments

This seems promising, I am using C# winforms in VS2013. I will check it out and let you know.
did the solution help?
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
thats simple, you just need to iterate through the listbox names and find, I've updated the answer above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.