So I need to make a textbox change background color every time I click a button, it needs to go from Red to green, from green to yellow etc.
Now, I know how to give it a color and how to change it, but instead of changing the colors in steps, it works down the entire list with a click of a button.
This is my code:
[CODE]private void button1_Click(object sender, EventArgs e)
{
if (textBox1.BackColor.Equals(System.Drawing.Color.Red))
textBox1.BackColor = System.Drawing.Color.Green;
if (textBox1.BackColor.Equals(System.Drawing.Color.Green))
textBox1.BackColor = System.Drawing.Color.Yellow;
if (textBox1.BackColor.Equals(System.Drawing.Color.Yellow))
textBox1.BackColor = System.Drawing.Color.Blue;
if (textBox1.BackColor.Equals(System.Drawing.Color.Blue))
textBox1.BackColor = System.Drawing.Color.Red;[/CODE]
I just started C# so some help would be appreciated.
Use else if.
- snip -
[QUOTE=SupahVee;19575625]Use else if.[/QUOTE]
Could you give me an example?
[CODE]private void button1_Click(object sender, EventArgs e)
{
if (textBox1.BackColor.Equals(System.Drawing.Color.Red))
textBox1.BackColor = System.Drawing.Color.Green;
else if (textBox1.BackColor.Equals(System.Drawing.Color.Green))
textBox1.BackColor = System.Drawing.Color.Yellow;
else if (textBox1.BackColor.Equals(System.Drawing.Color.Yellow))
textBox1.BackColor = System.Drawing.Color.Blue;
else if (textBox1.BackColor.Equals(System.Drawing.Color.Blue))
textBox1.BackColor = System.Drawing.Color.Red;[/CODE]
- snip -
Thanks, it works properly now. Rated everyone heart.
Wouldn't a switch be better here?
[code]switch (textBox1.BackColor) {
case System.Drawing.Color.Red:
// stuff, etc.
break;
default:
break;
}[/code]
Can you switch complex types?
[QUOTE=ShitBalls;19575988]Thanks, it works properly now. Rated everyone heart.[/QUOTE]
Another idea would be to have a list of all the possible colors and a counter variable. Every time its pressed, increase the counter variable and use the corresponding color. If the counter variable goes past the final color, just reset it to the first one.
[QUOTE=ZeekyHBomb;19577275]Can you switch complex types?[/QUOTE]
Enums in C#, although they're typed, are just int (by default).
Not that it matters though, because you can switch on pretty much any type in C#.
Maybe someone should explain why this didn't work.
You have to keep in mind that the computer reads the instructions from top to bottom.
In the first line you say: [B]If it's red, change it to green.[/B]
And in the second line it sais: [B]If it's green, change it to yellow.[/B]
and when read from the top to the bottom, you change from red to various other colors and then back to red.
That's why the value will stay the same all the time.
Instead of having a humongous switch/if statement(s) to change the colour, why not define a list of colours, and jump to the next item in the list?
[code]
List<System.Drawing.Color> colours = new List<System.Drawing.Color> {
System.Drawing.Color.Red,
System.Drawing.Color.Green,
System.Drawing.Color.Yellow,
System.Drawing.Color.Blue
};
private void button1_Click(object sender, EventArgs e)
{
int ci = colours.IndexOf(textBox1.BackColor);
if (ci == colours.Count - 1)
ci = -1;
textBox1.BackColor = colours[ci+1];
}
[/code]
[QUOTE=turby;19581827]Instead of having a humongous switch/if statement(s) to change the colour, why not define a list of colours, and jump to the next item in the list?
[code]
-snip-
[/code][/QUOTE]
Why use a List when the size doesn't change, you could just use an array.
But why use a collection at all when you could do what nullsquared said, just increment a counter and then compare it to the last enum, roll it back to 0 if it's bigger.
[QUOTE=jA_cOp;19582208]Why use a List when the size doesn't change, you could just use an array.
But why use a collection at all when you could do what nullsquared said, just increment a counter and then compare it to the last enum, roll it back to 0 if it's bigger.[/QUOTE]
That's pretty much what nullsqared said to do, except a lack of a counter variable.
Sorry, you need to Log In to post a reply to this thread.