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++; } } } }
时间: 2024-11-05 13:38:53