C# 为控件添加自定义事件,自定义触发

先随便搞个事件吧

 public class TestEventrgs : EventArgs
    {

        private string _name;
        public string Name { get { return _name; } }

        private int  _age;
        public int Age { get { return _age; } }
        public TestEventrgs(string name,int age)
        {
            _name = name;
            _age = age;
        }
    }

分两种,自定义控件和winfrom下的已有控件

先来个自定义控件吧
随便搞个界面
在这里插入图片描述

上马

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 CSDN
{
    public partial class UserControl1 : UserControl 
    {
        int ClickNuM = 0; //点击次数
        public event EventHandler<TestEventrgs> TestEventrg;//自定义的事件


        public UserControl1()
        {
            InitializeComponent();
            this.TestEventrg += new EventHandler<TestEventrgs>(DangeTip);//自定义事件绑定的方法
        }

        private void DangeTip(object sender, TestEventrgs e)
        {
            string tool = string.Format("危险提示:{0}你小子别狂点,仗着{1}岁手速快是吧!?",e.Name,e.Age);
            MessageBox.Show(tool);
        }

    

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            ClickNuM++;
            if (ClickNuM>5)
            {
                //触发自定义事件
                this.TestEventrg?.Invoke(this,new TestEventrgs("ming",17));//输入的参数可以自己传入
                ClickNuM = 0;
            }
        }

    }
}

放到界面上,狂点之后
在这里插入图片描述

接下来是winfrom下的已有控件,以button为例子

先添加一个组件

在这里插入图片描述
改为继承 Button,并添加相应的自定义事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CSDN
{
    public partial class MyButton : Button
    {
        public MyButton()
        {
            InitializeComponent();          
        }

        public event EventHandler<TestEventrgs> TestEventrg;
     
        public MyButton(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
    }
}

将组件从工具箱添加到界面,添加对应方法

在这里插入图片描述

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 CSDN
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        int ClickNuM = 0;

        private void myButton1_TestEventrg(object sender, TestEventrgs e)
        {
            string tool = string.Format("危险提示:{0}你小子别狂点,仗着{1}岁手速快是吧!?", e.Name, e.Age);
            MessageBox.Show(tool);
        }

        private void myButton1_Click(object sender, EventArgs e)
        {
            ClickNuM++;
            if (ClickNuM > 5)
            {               
              myButton1_TestEventrg(this, new TestEventrgs("lang", 88));
              ClickNuM = 0;
            }
        }
    }
}

运行之后,狂点。触发

在这里插入图片描述

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangzm996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值