zoukankan      html  css  js  c++  java
  • 步步为营-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++;
                }
            }
        }
    }
    View Code

    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("点击了三次");
            }
        }
    }
    View Code

    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++;
                }
            }
        }
    }
    View Code
  • 相关阅读:
    Hadoop技术内幕——Hadoop配置信息处理
    协程,线程池
    线程知识点(锁,信号量,队列,条件)
    进程的概念
    socketserver网络编程简单的小例子
    socket 发送文件
    网络编程
    正则表达式
    面对对象--多态封装
    创建可管理的属性:property
  • 原文地址:https://www.cnblogs.com/YK2012/p/6746890.html
Copyright © 2011-2022 走看看