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

StackOverflow https://stackoverflow.com/questions/11177893

  •  16-06-2021
  •  | 
  •  

Domanda


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

È stato utile?

Soluzione

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
                //...
            }
        }

Altri suggerimenti

ImageList is a component, not a control.
It does not get added to any collections.

Instead, you can make your own Dictionary<string, ImageList> yourself.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top