C# button按钮统一事件

来源:百度知道 编辑:UC知道 时间:2024/06/05 03:43:54
说明:splitContainer1.Panel2上有N个button
要求:鼠标进入(mouseEnter)任何一个button区域的时候,该button的字体变成红色,mouseLeave以后,恢复。但我不想为每个button一个一个加入mouseEnter和leave事件,要求写一个方法来实现。
C# 程序文件,非网页,窗体上有一个splitContainer1.Panel2,上面有好几个button。 比如有三个button:分别是btn1,btn2和btn3。
同意jonnysuen兄的说法,可是方法该如何写呢?应该通过什么事件来检测鼠标是停留在哪个button上呢?初学,很多东西还没搞懂,希望能给些相对较完整的代码,以供参考学习,谢谢!

一、在构造函数里或Form_Load事件加下面代码
foreach (Control ctrl in splitContainer1.Panel2.Controls)
{
if (ctrl is Button)
{
ctrl.MouseEnter += new EventHandler(ctrl_MouseEnter);
ctrl.MouseLeave += new EventHandler(ctrl_MouseLeave);
}
}
二、加下面两个方法
private void ctrl_MouseEnter(object sender, EventArgs e)
{
((Button)sender).ForeColor = Color.Red;
}
private void ctrl_MouseLeave(object sender, EventArgs e)
{
((Button)sender).ForeColor = Color.Black;//改成你默认的颜色
}

写个CSS样式表

你说的是CS还是BS啊,BS的话用input 然后以类型来判断。

方法可以只有一个,但事件必须每个按钮要有

写个SupperButton继承Button,实现mouseEnter和leave事件。

用SKIN吧
<asp:TextBox SkinId="easyto" BackColor="#EFEFEF" runat="server" style="text-align:right;border-bottom: 1px solid #000000; border-left: 0px solid yellow; border-right: 0px solid blue; border-top: 0px solid green; " ></asp:TextBox>