As in the title, my question is how does C# iterate through form controls in a
foreach(Control c in MyForm)
It doesn't return controls based on tab index, it returns the controls in the order you added them the
You can see this order in designer in Document Outline window. (Ctrl + Alt + T) and change the order by moving controls in tree in the Document Outline window. Also if you use
Bring To front and
Send To back toolbar buttons you can change this order in design time.
At run-time you can change this order by calling
BringToFront method of controls or
SetChildIndex method of
To get the index of a control, you can use
GetChildIndex method of
Also keep in mind
foreach(Control c in this.Controls) just will scan controls which you put directly on the form. For example if you have a panel on form and the panel contains some controls, children of panel are in
panel.Controls collection and not in controls collection of form.