Search code examples
c#formsloopsimagelist

C# How to get required control (ImageList) while iterating through contols on a Windows Form



While loading a Windows Form (C#), I'm trying to iterate through all controls. So I'm writing code in public Form1()

I've 5 ImageList controls on my form and I want to select an ImageList control on the basis of a string expression.

Can anyone please help to achieve this?

Thanks


Solution

  • ImageList is not a control so you can't find them back by iterating the form's Controls collection. "Selecting" is not a valid operation, assuming you mean setting the focus to it. It isn't visible at runtime.

    Find them back through the "components" field, like this:

            foreach (Component comp in this.components.Components) {
                var ilist = comp as ImageList;
                if (ilist != null) {
                    // Got one, do something with it
                    //...
                }
            }