首页 > 其他 > 详细

步步为营-29-三击事件

时间:2017-04-22 11:06:06      阅读:143      评论:0      收藏:0      [点我收藏+]

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

 

步步为营-29-三击事件

原文:http://www.cnblogs.com/YK2012/p/6746890.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!