Consult everybody, how to simplify the following c # scripts that can be incorporated into a foreach statement?
Foreach (Control CTRL in groupBox1. Controls) { If (CTRL is TextBox | | CTRL is ComboBox) CTRL. Text=""; }
Foreach (Control ctrl2 in panel1. Controls) { If (ctrl2 is TextBox | | ctrl2 is ComboBox) Ctrl2. Text=""; } Foreach (Control ctrl2 in panel2. Controls) { If (ctrl2 is TextBox | | ctrl2 is ComboBox) Ctrl2. Text=""; } Foreach (Control ctrl2 in panel3. Controls) { If (ctrl2 is TextBox | | ctrl2 is ComboBox) Ctrl2. Text=""; } Foreach (Control ctrl2 in panel4. Controls) { If (ctrl2 is TextBox | | ctrl2 is ComboBox) Ctrl2. Text=""; }
CodePudding user response:
Are not different controls, please set outside a layer of circulation
CodePudding user response:
List All=new List (a); All. AddRange (groupBox1. Controls);
The foreach (Control ctrl2 in all) { If (ctrl2 is TextBox | | ctrl2 is ComboBox) Ctrl2. Text=""; }
CodePudding user response:
I have an immature idea: first you need to modify the controls inherit out, and then traverse the
Public partial class Form1: Form { Private List _allControls;