码迷,mamicode.com
首页 > 其他好文 > 详细

步步为营-29-三击事件

时间:2017-04-22 12:50:16      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:box   win   event   ati   open   mod   public   成功   closed   

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-三击事件

标签:box   win   event   ati   open   mod   public   成功   closed   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!