多个按钮,一个onclick事件可更改单击按钮的颜色 [英] multiple buttons, one onclick event to change clicked button color

查看:166
本文介绍了多个按钮,一个onclick事件可更改单击按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用24个按钮(每天一小时一个按钮)来制作一个用户控件.如果用户单击按钮,则应该更改其颜色,我想获取特定按钮的名称.我想使用一个onclick事件来为所有按钮调用相同的功能(编写24次相同的按钮似乎效率不高).现在,我的问题是(除了缺乏知识外)我现在创建控件并单击一键调用以下事件:

I try to make a usercontrol with lets say 24 buttons, one button for every hour of a day. If a user clicks on a button it should change its color and I would like to get the name of the specific button. I would like to use one onclick event that calls the same function for all buttons (writing 24 times the same could doesn''t seem efficient).Now my problem is (beside lack of knowledge) that currently if I create the control and click one button it calls the following event:

public void single_button_Click(object sender, EventArgs e)
        {
            if (BackColor == Color.Red)
            {
                BackColor = SystemColors.Control;
            }
            if (BackColor == SystemColors.Control)
            {
                BackColor = Color.Red;
            }
        }



但这会更改控件的背景,而不是单击按钮的背景.
我可以为每个按钮创建一个函数,并使用btn1.BackColor为例,但是这样做效率不高(相同代码的24倍).
我该如何解决?
另外,我想获得被单击的按钮的名称或文本,如何在通用函数中实现呢?
谢谢您的帮助!



But this changes the background of the control, not of clicked button.
I could make a function for each button and use btn1.BackColor for example, but that would be inefficient (24 times the same code).
How can I solve this?
Additionaly I would like to get the name or text of the button that was clicked, how can I achieve that in a generic function?
Thanks for you help!

推荐答案

您需要确定发送消息的按钮是哪个,以便仅更改该按钮的颜色.您可以通过将sender参数强制转换为Button来实现,例如:
You need to identify which button sent the message in order to change the colour of only that button. You do that by casting the sender parameter to Button, something like:
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;
  }
}


这是我刚刚为此删除的代码.

在我的示例中,我在表单上有一个包含按钮的面板.

所有按钮.单击事件指向相同的单击功能;

Here is the code i just knocked up for doing this.

In my example i have a panel on the form which contains the buttons.

All the buttons.Click events point to the same click function;

private void button_Click(object sender, EventArgs e)
{

    //set all the controls in the panel to the same backcolor
    foreach (Control c in panel1.Controls)
    {
        c.BackColor = Color.DimGray;
    }
    //set the clicked control to a different color
    Control o = (Control)sender;
    o.BackColor = Color.Green;
}



单击一个按钮后,所有按钮都将重置为相同的颜色,然后单击的按钮将更改其颜色.



When a button is clicked, all the buttons are reset to the same color, then the button that was clicked has its colour changed.


这篇关于多个按钮,一个onclick事件可更改单击按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆