当前位置 : 主页 > 网络编程 > c#编程 >

C#如何绑定多个按钮到同一个事件

来源:互联网 收集:自由互联 发布时间:2023-01-31
目录 绑定多个按钮到同一个事件 1.添加代码 2.选择你要添加代码的控件 多个控件有同一个事件,优化 先自己声明方法,再用事件调用 绑定多个按钮到同一个事件 1.添加代码 private void c
目录
  • 绑定多个按钮到同一个事件
    • 1.添加代码
    • 2.选择你要添加代码的控件
  •  多个控件有同一个事件,优化
    • 先自己声明方法,再用事件调用

绑定多个按钮到同一个事件

1.添加代码

private void clauseElementClicked(object sender, EventArgs e)
{undefined
textBoxWhereClause.SelectedText = ((Button)sender).Text;
}

2.选择你要添加代码的控件

右键–属性–找到click这个事件,让后选择clauseElementClicked这个事件就可以了。 

 多个控件有同一个事件,优化

先自己声明方法,再用事件调用

public void borderS3D(object sender,EventArgs e)
        {
               ((PictureBox)sender).BorderStyle=BorderStyle.Fixed3D;         
        } public void borderSNo(object sender, EventArgs e)
        {
            ((PictureBox)sender).BorderStyle = BorderStyle.None;
        }
public FormMain()
        {
            InitializeComponent();
            pictureBox1.MouseHover += new  EventHandler(borderS3D);
            pictureBox1.MouseLeave += new EventHandler(borderSNo);
            pictureBox2.MouseHover += new EventHandler(borderS3D);
            pictureBox2.MouseLeave += new EventHandler(borderSNo);
            pictureBox3.MouseHover += new EventHandler(borderS3D);
            pictureBox3.MouseLeave += new EventHandler(borderSNo);
            pictureBox4.MouseHover += new EventHandler(borderS3D);
            pictureBox4.MouseLeave += new EventHandler(borderSNo);
            pictureBox5.MouseHover += new EventHandler(borderS3D);
            pictureBox5.MouseLeave += new EventHandler(borderSNo);
            pictureBox6.MouseHover += new EventHandler(borderS3D);
            pictureBox6.MouseLeave += new EventHandler(borderSNo);
            pictureBox7.MouseHover += new EventHandler(borderS3D);
            pictureBox7.MouseLeave += new EventHandler(borderSNo);
            pictureBox8.MouseHover += new EventHandler(borderS3D);
            pictureBox8.MouseLeave += new EventHandler(borderSNo);
            pictureBox9.MouseHover += new EventHandler(borderS3D);
            pictureBox9.MouseLeave += new EventHandler(borderSNo);
            pictureBox10.MouseHover += new EventHandler(borderS3D);
            pictureBox10.MouseLeave += new EventHandler(borderSNo);
            pictureBox11.MouseHover += new EventHandler(borderS3D);
            pictureBox11.MouseLeave += new EventHandler(borderSNo);
           
        }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

上一篇:C#winform中数据库绑定DataGrid的实现
下一篇:没有了
网友评论