简体   繁体   English

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

[英]How to change only one dynamic button's backcolor when clicked once and then again?

I have created 9 buttons dynamically and each with its own click event. 我动态创建了9个按钮,每个按钮都有自己的click事件。

When a Button is clicked, it changes to red. 单击一个按钮后,它将变为红色。 If the Button is re-clicked, the button changes back to light gray. 如果重新单击该按钮,该按钮将变回浅灰色。

For some reason when I click one button, all the other buttons change to red as well. 由于某些原因,当我单击一个按钮时,所有其他按钮也会变为红色。 How do I make it so that only the one that is clicked is red? 我如何做到只有被点击的那个是红色的?

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

This can be compressed to one Click handler that you register with all Buttons. 可以将其压缩为向所有按钮注册的一个Click处理程序。 The trick is that the button that raised the event is transmitted in the "sender" argument. 诀窍是引发事件的按钮在“发送者”参数中传输。 You just need to cast it, to have full access to the specific button: 您只需要强制转换即可完全访问特定按钮:

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

Using switch...case with Pattern matching or maybe an array, you might even be able to have them properly cycle through a list without too much code. 使用带模式匹配的switch ... case或一个数组,您甚至可以使它们在列表中正确循环而无需太多代码。

Of course if you then later need to extract wich color the Button is on, that sounds like a faulty UI/UX design. 当然,如果您以后需要提取颜色时,则打开按钮,这听起来像是错误的UI / UX设计。 Generally the values you need for calculations/opeartions should be kept in code behind. 通常,计算/运算所需的值应保留在代码中。 With the UI elements being meerely representations of the current values. UI元素仅代表当前值。 That way you do not have to go and parse properties of Buttons to get the user selected values. 这样,您不必去解析Buttons的属性即可获取用户选择的值。

I found an answer that works: 我找到了一个有效的答案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ObjectListView中更改按钮背景色 - How to change a button backcolor in ObjectListView 如何在UWP中更改按钮的背景色 - How to change the backcolor of a Button in UWP 在窗口窗体中聚焦时更改按钮的背景色 - Change BackColor of button when focus in Window Form 如何在按下时更改按钮的背景颜色并使用另一个按钮将颜色更改为另一种颜色 - How to change backcolor of a button when pressed and change the color to another color using another button 单击按钮时更改标签的文本 - Change Label's Text when Button is clicked 如何在运行时在代码块结束之前更改按钮的backColor - How to change the button's backColor before the end of the block of code at run time 当 DropdownStyle 为 DropDownList 时,如何更改 ComboBox 的背景色? - How to change the BackColor of a ComboBox when DropdownStyle is DropDownList? 单击后如何禁用ASP.NET WebForms按钮,并在回发完成后再次启用它? - How would one disable an ASP.NET WebForms button when its clicked, and have it be enabled again after postback completes? 单击按钮时如何改变轨道方向? - how to change orbit direction when button clicked? 如何在Xaml中单击时更改按钮内容? - How to change button content when clicked in Xaml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM