Prit
Prit

Reputation: 25

Change panels color on button click

How to change panels color on button_click event? Below is code snippet, I want to change color of panel1 when I click on Clear button.

private void buttClear_Click(object sender, EventArgs e)
{       
     txtPntX.Text = "";
     txtPntY.Text = "";

     txtSrtPtX.Text = "";
     txtSrtPtY.Text = "";
     txtEndPtX.Text = "";
     txtEndPtY.Text = "";
}

Upvotes: 0

Views: 1592

Answers (3)

m.lanser
m.lanser

Reputation: 484

Why don´t just use:

private void buttClear_Click(object sender, EventArgs e)
{       
     txtPntX.Text = "";
     txtPntY.Text = "";
     txtSrtPtX.Text = "";
     txtSrtPtY.Text = "";
     txtEndPtX.Text = "";
     txtEndPtY.Text = "";
     panel1.BackColor = Color.Red; // < This one
}

Upvotes: 0

Dn24Z
Dn24Z

Reputation: 149

If you want to change a color every time the txtSrtPtX textbox text changes, you need to add an event TextChanged:

private void txtSrtPtX_TextChanged(object sender, EventArgs e)
{
      panel1.BackColor = Color.Red;
}

The panel in your case will change color 6 times:

private void buttClear_Click(object sender, EventArgs e)
{       
     txtPntX.Text = ""; //1
     txtPntY.Text = ""; //2    
     txtSrtPtX.Text = ""; //3
     txtSrtPtY.Text = ""; //4 
     txtEndPtX.Text = ""; //5
     txtEndPtY.Text = ""; //6
}

Upvotes: 0

nvoigt
nvoigt

Reputation: 77304

What's keeping you from doing it?

panel1.BackColor = Colors.Red;

Upvotes: 1

Related Questions