1. Looping traversal
private void GetControls(Control fatherControl) { sonControls = ; foreach (Control control in sonControls) { (); } }
Result: You can obtain Panel, GroupBox, TabControl and other controls
Problem: The child controls on Panel and other controls cannot be obtained
2. Recursive traversal
private void GetControls(Control fatherControl) { sonControls = ; foreach (Control control in sonControls) { (); if ( != null) { GetControls(control); } } }
Result: Most controls can be obtained
Problem: Timer, ContextMenuStrip and other controls cannot be obtained
3. Use reflection
private void GetControls(Control fatherControl) { [] fieldInfo = ().GetFields( | ); for (int i = 0; i < ; i++) { (fieldInfo[i].Name); } }
Result: All controls are retrieved
DevExpress
The control cannot be usedIt can only be obtained by reflection method, as follows:
public void SearchBarManager() { Type FormType = (); FieldInfo[] fi = ( | | ); foreach (FieldInfo info in fi) { if ( == typeof()) { bar = ((this)) as ; foreach ( bi in ) { (); } } } }
The above is the detailed content of the three methods of C# WinForm traversing form controls. For more information about WinForm traversing form controls, please pay attention to my other related articles!