C#如何绑定多个按钮到同一个事件
短信预约 -IT技能 免费直播动态提醒
绑定多个按钮到同一个事件
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);
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341