一次又一次单击时,如何仅更改一个动态按钮的背景色?

时间:2018-12-05 02:05:47

标签: c# winforms button dynamic backcolor

我动态创建了9个按钮,每个按钮都有自己的click事件。

单击按钮时,它会变为红色。如果重新单击该按钮,该按钮将变回浅灰色。

由于某些原因,当我单击一个按钮时,所有其他按钮也会变为红色。 如何做到只有被点击的那个是红色的?

    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;
    }

2 个答案:

答案 0 :(得分:0)

可以将其压缩为您向所有按钮注册的一个Click处理程序。诀窍是引发事件的按钮在“发送者”参数中传输。您只需要进行投射即可完全访问特定按钮:

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 ...具有模式匹配或数组的情况,您甚至可以使它们在列表中正确循环而无需太多代码。

当然,如果您以后需要提取颜色时,则打开按钮,这听起来像是错误的UI / UX设计。通常,计算/运算所需的值应保留在代码中。 UI元素仅是当前值的表示。这样,您不必去解析Buttons的属性即可获取用户选择的值。

答案 1 :(得分:0)

我找到了一个可行的答案:

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;
  }
}