如何在button_click事件中更改面板颜色?下面是代码片段,我想在点击“清除”按钮时更改panel1
的颜色。
private void buttClear_Click(object sender, EventArgs e)
{
txtPntX.Text = "";
txtPntY.Text = "";
txtSrtPtX.Text = "";
txtSrtPtY.Text = "";
txtEndPtX.Text = "";
txtEndPtY.Text = "";
}
答案 0 :(得分:1)
什么阻止你这样做?
panel1.BackColor = Colors.Red;
答案 1 :(得分:0)
如果您想在每次txtSrtPtX
文本框文本更改时更改颜色,则需要添加事件TextChanged
:
private void txtSrtPtX_TextChanged(object sender, EventArgs e)
{
panel1.BackColor = Color.Red;
}
您案例中的面板将改变颜色6次:
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
}
答案 2 :(得分:0)
为什么不使用:
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
}