Home > Net >  How can the code concise?
How can the code concise?

Time:11-01

If (group. The Contains (Convert. ToInt32 (button3. Text)))
{
Button3. ForeColor=Color. White;
Button3. BackColor=Color. Crimson;
}
The else
{
Button3. ForeColor=Color. Black;
Button3. BackColor=Color. Transparent;
}

A total of 120 button need according to the group, whether they contain text values to change the color of the button

Can't write 120 if statement?

How can I clean? thank you

CodePudding user response:

Is this a loop line?

CodePudding user response:

 foreach (Control in Controls, c) {
The Button BTN=c as Button;
If (BTN==null)
continue;
If (group. The Contains (Convert. ToInt32 (BTN. Text)))
{
BTN. ForeColor=Color. White;
BTN. BackColor=Color. Crimson;
}
The else
{
BTN. ForeColor=Color. Black;
BTN. BackColor=Color. Transparent;
}
}

CodePudding user response:

 void test (HashSet Buttons) 
{
The foreach (var button3 buttons) in
{
If (group. The Contains (Convert. ToInt32 (button3. Text)))
{
Button3. ForeColor=Color. White;
Button3. BackColor=Color. Crimson;
}
The else
{
Button3. ForeColor=Color. Black;
Button3. BackColor=Color. Transparent;
}
}
}

CodePudding user response:

In a nutshell, the so-called "how to simple?" This kind of problem, is to "write a code only!" "

For the simple model, design Method, for complex patterns, design for a Class, and then reuse,

Can reuse, to god,

CodePudding user response:

For example winform all controls support holding structure, but because of various kinds of "class" teaching is less than the degree of knowledge can only teach some fur, and we are really very few winform and too lazy to use some knowledge, then we can "invented" some Helper methods reinvent car wheels, to achieve "just write a code", such as the Button type of object to write a name for SetColor extension methods, so you can in the code with concise code to call at any time, and will "multi-line" code hidden (packaging),
  •  Tags:  
  • C#
  • Related