C # traverse Form Controls (original from http://www.liangshunet.com/ca/201403/286434593.htm ),
I. C # traverse Form Controls
It mainly traverses the Form Control (Controls). If the Form contains Panel, Button, and TextBox Controls, the traversal code is as follows:
/// <Summary> /// Winform C # traverse the Form Control /// </summary> /// <param name = "ctrlName"> Control name </param> public void ForeachFormControls (string ctrlName) {foreach (Control ctrl in this. controls) {if (ctrl is Panel) {// operation code ctrl. backColor = Color. aquamarine;} else if (ctrl is Button) {ctrl. foreColor = Color. royalBlue;} else if (ctrl is TextBox) {ctrl. text = null;} // you can specify if (ctrl. name. equals (ctrlName) {vctrl. name = string. empty ;}}}
The above Code only performs some random operations based on the control type and the name of the passed control. You can change it to the required operation as needed.
II. C # Winform traversal control-Traverse sub-controls in the Panel
The Traversal method is the same as the control in the traversal form, except that this must be changed to the Panel name. If the Panel control only contains the Button and TextBox sub-controls, the Code is as follows:
/// <Summary> /// C # traverse the sub-control /// </summary> /// <param name = "ctrlName"> Control name </param> public void foreachPanelControls (string ctrlName) {foreach (Control ctrl in panel1.Controls) {if (ctrl is Button) {if (ctrl. name. equals (ctrlName) ctrl. foreColor = Color. royalBlue; else ctrl. foreColor = Color. skyBlue;} else if (ctrl is TextBox) {if (ctrl. name. equals (ctrlName) ctrl. name = "Current Value"; else ctrl. text = null ;}}}