Home > Net >  C # script to simplify the demand
C # script to simplify the demand

Time:11-28

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;

Public _click ()
{
InitializeComponent ();
}

Private void Form1_Load (object sender, EventArgs e)
{
GetAllControls ();
}

Private void GetAllControls ()
{
Foreach (Control ctrl1 in Controls)
{
_allControls. Add (ctrl1);
}

Foreach (Control ctrl2 in panel1. Controls)
{
_allControls. Add (ctrl2);
}

Foreach (Control ctrl3 in panel2. Controls)
{
_allControls. Add (ctrl3);
}
}

Private void SetMyControlValue ()
{
The foreach (Control CTRL in _allControls)
{
If (CTRL is MyTextBox | | CTRL is MyComboBox)
{
CTRL. Text="";
}
}
}
}

CodePudding user response:

refer to the second floor icoolno1 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="";
}


List All=new List (a);
All. AddRange (groupBox1. Controls);
All. AddRange (groupBox2. Controls);
All. AddRange (groupBox3. Controls);
All. AddRange (groupBox4. Controls);

The foreach (Control ctrl2 in all)
{
If (ctrl2 is TextBox | | ctrl2 is ComboBox)
Ctrl2. Text="";
}
[/code]

CodePudding user response:

Don't have to be simplified, not worth to simplify
Programmers should focus on the completion of the most worth the time things

It is not worth it, and done, you will find more depressed, tomorrow you with his head to see the result, the leadership of the head, pinch the psoas muscle said, "well, I think, the pendulum is here, the place where the adjust the position, and the the drop-down box gave a hint, this textbox make a dash"

Upstairs several waste strength simplified brothers, you cry not to cry

CodePudding user response:

reference 5 floor wanghui0380 reply:
don't have to simplify, it's not worth to simplify
Programmers should focus on the completion of the most worth the time things

It is not worth it, and done, you will find more depressed, tomorrow you with his head to see the result, the leadership of the head, pinch the psoas muscle said, "well, I think, the pendulum is here, the place where the adjust the position, and the the drop-down box gave a hint, this textbox make a dash"

Upstairs several waste strength simplified brothers, you cry cry


, we only solve the problems of the building Lord,
  •  Tags:  
  • C#
  • Related