首页 > 代码库 > 步步为营-29-三击事件
步步为营-29-三击事件
1:按钮的三击事件可能在多个地方使用,所以设置为用户控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 三击事件 { public partial class ThreeClick : UserControl { public event EventHandler threeClick; public ThreeClick() { InitializeComponent(); } int n = 0; private void btnThree_Click(object sender, EventArgs e) { if (n == 2) { if (threeClick!=null) { threeClick(this,e); } n = 0; } else { n++; } } } }
2在form中添加用户控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 三击事件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void threeClick1_Load(object sender, EventArgs e) { threeClick1.threeClick += threeClick1_threeClick; } void threeClick1_threeClick(object sender, EventArgs e) { MessageBox.Show("点击了三次"); } } }
3 一个更简单的写法
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 三击事件 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } Button btn = new Button(); public void Form2_Load(object sender, EventArgs e) { btn.Location = new Point(100,100); btn.Size = new Size(50,50); btn.Text="点击"; this.Controls.Add(btn); btn.Click += btn_Click; } int n = 0; void btn_Click(object sender, EventArgs e) { if (n == 3) { MessageBox.Show("三击成功"); n = 0; } else { n++; } } } }
步步为营-29-三击事件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。