C#如何点击一个按纽,其余按纽恢复默认色

c#, 有12个按纽,默认色为蓝色,点击按纽变成橙色,然后,再点击其他任何一个按纽时,之前点击的按纽的橙色变为默认蓝(意思是只允许被点击的变橙,其他均需要恢复默认蓝)。如果我foreach每个按纽颜色,就需要每个按纽的click事件都添加这个检测代码,有简单的方法吗?另外,我的按纽设定有鼠标悬停变色功能(用MouseMove和MouseLeave实现的),被点击变橙的按纽如果鼠标经过,马上恢复mouseleave设定的蓝色,怎么处理。

你可以参考一下:
1、定义一个Button类的子类,在子类中实现你要的所有功能。
进菜单,工程-添加-新建项,新建一个如下的C#类:
public class MyButton : System.Windows.Forms.Button
{
public MyButton()
{
this.BackColor=System.Drawing.Color.Blue;
this.Click+=delegate(object sender, EventArgs e)
{
this.BackColor=System.Drawing.Color.Orange;
MyButton.ClickedButton=this;
};
this.MouseEnter+=delegate(object sender, EventArgs e)
{
if(null!=MyButton.ClickedButton)
{
MyButton.ClickedButton.BackColor=System.Drawing.Color.Blue;
}
this.BackColor=System.Drawing.Color.Orange;
};
this.MouseLeave+=delegate(object sender, EventArgs e)
{
if(null!=MyButton.ClickedButton)
{
MyButton.ClickedButton.BackColor=System.Drawing.Color.Orange;
}
if(MyButton.ClickedButton!=this)
{
this.BackColor=System.Drawing.Color.Blue;
}
};
}
private static MyButton ClickedButton=null;
}
2、在窗体的“工具”中,选择“自定义组件”,将若干个MyButton拖到窗体中。
温馨提示:答案为网友推荐,仅供参考
相似回答