一次又一次单击时如何仅更改一个动态按钮的背景色?
How to change only one dynamic button's backcolor when clicked once and then again?
我动态创建了 9 个按钮,每个按钮都有自己的点击事件。
单击按钮时,它会变为红色。如果再次单击该按钮,该按钮将变回浅灰色。
出于某种原因,当我单击一个按钮时,所有其他按钮也都变为红色。
怎么才能让被点击的那个是红色的?
private void frmToppings_Load(object sender, EventArgs e)
{
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
formXsize = this.Size.Width;
formYsize = this.Size.Height;
path = dir + "deli1.jpg";
Image img = Image.FromFile(path, true);
this.BackgroundImage = img;
this.BackgroundImageLayout = ImageLayout.Stretch;
this.btns[0] = new Point(formXsize / 6 - formXsize / 7, formYsize / 3 - formYsize / 8);
this.btns[1] = new Point(formXsize / 3 - formXsize / 7, formYsize / 3 - formYsize / 8);
this.btns[2] = new Point(formXsize / 2 - formXsize / 7, formYsize / 3 - formYsize / 8);
this.btns[3] = new Point(formXsize / 6 - formXsize / 7, formYsize / 2 - formYsize / 8);
this.btns[4] = new Point(formXsize / 3 - formXsize / 7, formYsize / 2 - formYsize / 8);
this.btns[5] = new Point(formXsize / 2 - formXsize / 7, formYsize / 2 - formYsize / 8);
this.btns[6] = new Point(formXsize / 6 - formXsize / 7, formYsize / 1 - formYsize / 5);
this.btns[7] = new Point(formXsize / 3 - formXsize / 7, formYsize / 1 - formYsize / 5);
this.btns[8] = new Point(formXsize / 2 - formXsize / 7, formYsize / 1 - formYsize / 5);
this.btnSize = new Size(formXsize / 7, formYsize / 7);
this.txt[0] = new Point(formXsize / 2 + 70, formYsize / 10 - formYsize / 12);
this.txtSize = new Size(formXsize - 950, formYsize - 40);
this.Controls.Clear();
DrawHoagieToppingsForm();
}
public void DrawHoagieToppingsForm()
{
Button btnMayo = new Button();
btnMayo.Text = "Mayo";
btnMayo.Location = btns[0];
btnMayo.Size = btnSize;
btnMayo.Click += new EventHandler(btnMayo_Click);
btnMayo.BackColor = SystemColors.Control;
Controls.Add(btnMayo);
Button btnOil = new Button();
btnOil.Text = "Oil";
btnOil.Location = btns[1];
btnOil.Size = btnSize;
btnOil.Click += new EventHandler(btnOil_Click);
btnOil.BackColor = SystemColors.Control;
Controls.Add(btnOil);
Button btnOnion = new Button();
btnOnion.Text = "Onion";
btnOnion.Location = btns[2];
btnOnion.Size = btnSize;
btnOnion.Click += new EventHandler(btnOnion_Click);
btnOnion.BackColor = SystemColors.Control;
Controls.Add(btnOnion);
Button btnHotPeppers = new Button();
btnHotPeppers.Text = "Hot Peppers";
btnHotPeppers.Location = btns[3];
btnHotPeppers.Size = btnSize;
btnHotPeppers.Click += new EventHandler(btnHotPeppers_Click);
btnHotPeppers.BackColor = SystemColors.Control;
Controls.Add(btnHotPeppers);
Button btnSweetPeppers = new Button();
btnSweetPeppers.Text = "Sweet Peppers";
btnSweetPeppers.Location = btns[4];
btnSweetPeppers.Size = btnSize;
btnSweetPeppers.Click += new EventHandler(btnSweetPeppers_Click);
btnSweetPeppers.BackColor = SystemColors.Control;
Controls.Add(btnSweetPeppers);
Button btnOregano = new Button();
btnOregano.Text = "Oregano";
btnOregano.Location = btns[5];
btnOregano.Size = btnSize;
btnOregano.Click += new EventHandler(btnOregano_Click);
btnOregano.BackColor = SystemColors.Control;
Controls.Add(btnOregano);
TextBox txtReceipt = new TextBox();
txtReceipt.Multiline = true;
txtReceipt.ReadOnly = true;
txtReceipt.Location = txt[0];
txtReceipt.Size = txtSize;
Controls.Add(txtReceipt);
Button btnBack = new Button();
btnBack.Text = "Back";
btnBack.Location = btns[6];
btnBack.Size = btnSize;
btnBack.Click += new EventHandler(btnBack_Click);
Controls.Add(btnBack);
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.Location = btns[7];
btnCancel.Size = btnSize;
btnCancel.Click += new EventHandler(btnCancel_Click);
Controls.Add(btnCancel);
Button btnAddToOrder = new Button();
btnAddToOrder.Text = "Add to Order";
btnAddToOrder.Location = btns[8];
btnAddToOrder.Size = btnSize;
btnAddToOrder.Click += new EventHandler(btnAddToOrder_Click);
Controls.Add(btnAddToOrder);
}
private void btnMayo_Click(object sender, EventArgs e)
{
this.BackColor = Color.Red;
}
private void btnOil_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnOnion_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnHotPeppers_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnSweetPeppers_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnOregano_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
这可以压缩到您向所有 Button 注册的一个 Click 处理程序。诀窍是引发事件的按钮在 "sender" 参数中传输。您只需投射它,即可完全访问特定按钮:
private void btnCycleColors(object sender, EventArgs e)
{
//This cast will fail if the event is registered with anything that is not a Button
//But usually we can ignore that, as this will be quickly noticed
Button caller = (Button)sender;
if(caller.BackColor == X)
caller.BackColor = Y;
}
将 switch...case 与模式匹配或数组一起使用,您甚至可以让它们在列表中正确循环,而无需太多代码。
当然,如果您稍后需要提取按钮的颜色,这听起来像是一个错误的 UI/UX 设计。通常,calculations/opeartions 所需的值应保存在代码后面。 UI 元素仅仅是当前值的 表示 。这样您就不必去解析按钮的属性来获取用户选择的值。
我找到了有效的答案:
public void single_button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.BackColor == Color.Red)
{
btn.BackColor = SystemColors.Control;
}
else if (btn.BackColor == SystemColors.Control)
{
btn.BackColor = Color.Red;
}
}
我动态创建了 9 个按钮,每个按钮都有自己的点击事件。
单击按钮时,它会变为红色。如果再次单击该按钮,该按钮将变回浅灰色。
出于某种原因,当我单击一个按钮时,所有其他按钮也都变为红色。 怎么才能让被点击的那个是红色的?
private void frmToppings_Load(object sender, EventArgs e)
{
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
formXsize = this.Size.Width;
formYsize = this.Size.Height;
path = dir + "deli1.jpg";
Image img = Image.FromFile(path, true);
this.BackgroundImage = img;
this.BackgroundImageLayout = ImageLayout.Stretch;
this.btns[0] = new Point(formXsize / 6 - formXsize / 7, formYsize / 3 - formYsize / 8);
this.btns[1] = new Point(formXsize / 3 - formXsize / 7, formYsize / 3 - formYsize / 8);
this.btns[2] = new Point(formXsize / 2 - formXsize / 7, formYsize / 3 - formYsize / 8);
this.btns[3] = new Point(formXsize / 6 - formXsize / 7, formYsize / 2 - formYsize / 8);
this.btns[4] = new Point(formXsize / 3 - formXsize / 7, formYsize / 2 - formYsize / 8);
this.btns[5] = new Point(formXsize / 2 - formXsize / 7, formYsize / 2 - formYsize / 8);
this.btns[6] = new Point(formXsize / 6 - formXsize / 7, formYsize / 1 - formYsize / 5);
this.btns[7] = new Point(formXsize / 3 - formXsize / 7, formYsize / 1 - formYsize / 5);
this.btns[8] = new Point(formXsize / 2 - formXsize / 7, formYsize / 1 - formYsize / 5);
this.btnSize = new Size(formXsize / 7, formYsize / 7);
this.txt[0] = new Point(formXsize / 2 + 70, formYsize / 10 - formYsize / 12);
this.txtSize = new Size(formXsize - 950, formYsize - 40);
this.Controls.Clear();
DrawHoagieToppingsForm();
}
public void DrawHoagieToppingsForm()
{
Button btnMayo = new Button();
btnMayo.Text = "Mayo";
btnMayo.Location = btns[0];
btnMayo.Size = btnSize;
btnMayo.Click += new EventHandler(btnMayo_Click);
btnMayo.BackColor = SystemColors.Control;
Controls.Add(btnMayo);
Button btnOil = new Button();
btnOil.Text = "Oil";
btnOil.Location = btns[1];
btnOil.Size = btnSize;
btnOil.Click += new EventHandler(btnOil_Click);
btnOil.BackColor = SystemColors.Control;
Controls.Add(btnOil);
Button btnOnion = new Button();
btnOnion.Text = "Onion";
btnOnion.Location = btns[2];
btnOnion.Size = btnSize;
btnOnion.Click += new EventHandler(btnOnion_Click);
btnOnion.BackColor = SystemColors.Control;
Controls.Add(btnOnion);
Button btnHotPeppers = new Button();
btnHotPeppers.Text = "Hot Peppers";
btnHotPeppers.Location = btns[3];
btnHotPeppers.Size = btnSize;
btnHotPeppers.Click += new EventHandler(btnHotPeppers_Click);
btnHotPeppers.BackColor = SystemColors.Control;
Controls.Add(btnHotPeppers);
Button btnSweetPeppers = new Button();
btnSweetPeppers.Text = "Sweet Peppers";
btnSweetPeppers.Location = btns[4];
btnSweetPeppers.Size = btnSize;
btnSweetPeppers.Click += new EventHandler(btnSweetPeppers_Click);
btnSweetPeppers.BackColor = SystemColors.Control;
Controls.Add(btnSweetPeppers);
Button btnOregano = new Button();
btnOregano.Text = "Oregano";
btnOregano.Location = btns[5];
btnOregano.Size = btnSize;
btnOregano.Click += new EventHandler(btnOregano_Click);
btnOregano.BackColor = SystemColors.Control;
Controls.Add(btnOregano);
TextBox txtReceipt = new TextBox();
txtReceipt.Multiline = true;
txtReceipt.ReadOnly = true;
txtReceipt.Location = txt[0];
txtReceipt.Size = txtSize;
Controls.Add(txtReceipt);
Button btnBack = new Button();
btnBack.Text = "Back";
btnBack.Location = btns[6];
btnBack.Size = btnSize;
btnBack.Click += new EventHandler(btnBack_Click);
Controls.Add(btnBack);
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.Location = btns[7];
btnCancel.Size = btnSize;
btnCancel.Click += new EventHandler(btnCancel_Click);
Controls.Add(btnCancel);
Button btnAddToOrder = new Button();
btnAddToOrder.Text = "Add to Order";
btnAddToOrder.Location = btns[8];
btnAddToOrder.Size = btnSize;
btnAddToOrder.Click += new EventHandler(btnAddToOrder_Click);
Controls.Add(btnAddToOrder);
}
private void btnMayo_Click(object sender, EventArgs e)
{
this.BackColor = Color.Red;
}
private void btnOil_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnOnion_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnHotPeppers_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnSweetPeppers_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
private void btnOregano_Click(object sender, EventArgs e)
{
//this.BackColor = Color.Red;
}
这可以压缩到您向所有 Button 注册的一个 Click 处理程序。诀窍是引发事件的按钮在 "sender" 参数中传输。您只需投射它,即可完全访问特定按钮:
private void btnCycleColors(object sender, EventArgs e)
{
//This cast will fail if the event is registered with anything that is not a Button
//But usually we can ignore that, as this will be quickly noticed
Button caller = (Button)sender;
if(caller.BackColor == X)
caller.BackColor = Y;
}
将 switch...case 与模式匹配或数组一起使用,您甚至可以让它们在列表中正确循环,而无需太多代码。
当然,如果您稍后需要提取按钮的颜色,这听起来像是一个错误的 UI/UX 设计。通常,calculations/opeartions 所需的值应保存在代码后面。 UI 元素仅仅是当前值的 表示 。这样您就不必去解析按钮的属性来获取用户选择的值。
我找到了有效的答案:
public void single_button_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.BackColor == Color.Red)
{
btn.BackColor = SystemColors.Control;
}
else if (btn.BackColor == SystemColors.Control)
{
btn.BackColor = Color.Red;
}
}