How to get control index in WinForms?

I have a panel with a set of controls. How can I get the index of a specific control when iterating through them? I use foreachto repeat, but there is no Index property. Should I use for x = 0...and return xwhen my match is done, or what?

+3
source share
3 answers

You can use:

panel.Controls.IndexOf(control);

Or you can iterate over them with a for loop instead of a foreach loop. Or you can simply create an index that you increment inside the foreach loop.

+7
source

You can simply use the IndexOf method. Something like panel1.Controls.IndexOf (textBox1);

+1

, ,

(x = 0; x < panel.Controls.Count; ++)

However, if you are dynamically viewing controls in a panel, you might consider providing them with unique names or other identification attributes through the .Name or .Tag properties.

You can then more clearly distinguish your child controls.

Hope this helps ...

0
source

All Articles