这篇文章主要介绍了C#如何绑定多个按钮到同一个事件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
绑定多个按钮到同一个事件
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#如何绑定多个按钮到同一个事件
基础教程推荐
猜你喜欢
- C#类和结构详解 2023-05-30
- ZooKeeper的安装及部署教程 2023-01-22
- C#控制台实现飞行棋小游戏 2023-04-22
- C# windows语音识别与朗读实例 2023-04-27
- unity实现动态排行榜 2023-04-27
- C# 调用WebService的方法 2023-03-09
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- winform把Office转成PDF文件 2023-06-14
- 一个读写csv文件的C#类 2022-11-06
- C# List实现行转列的通用方案 2022-11-02