This code changes the textbox instantly to red. I want it like, click button then red, again then green.
private void button1_Click(object sender, EventArgs e)
{
textBox1.BackColor = System.Drawing.Color.Black;
if (textBox1.BackColor.Equals(System.Drawing.Color.Black))
{
textBox1.BackColor = System.Drawing.Color.Red;
}
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.Blue;
}
if (textBox1.BackColor.Equals(System.Drawing.Color.Blue))
{
textBox1.BackColor = System.Drawing.Color.Red;
}
}
You want to use an
else if:What you were doing is changing it to
red, then checking if it isredand changing it togreen. By usingelse ifyou won’t execute theif redif it isblack.Also, as Tim points out in the comments, you need to remove the line
textBox1.BackColor = System.Drawing.Color.Blackto stop if from beenblackon every click. Set it toblackin your form’s constructor.