欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#為控件添加自定義事件及自定義觸發(fā)

 更新時(shí)間:2022年04月29日 09:12:38   作者:Code- Sheep  
C#本身提供了很強(qiáng)大的控件庫,但是很多控件庫的功能只是一些基本的功能,本文主要介紹了C#為控件添加自定義事件及自定義觸發(fā),具有一定的參考價(jià)值,感興趣的可以了解一下

先隨便搞個(gè)事件吧

 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下的已有控件

先來個(gè)自定義控件吧
隨便搞個(gè)界面

在這里插入圖片描述

上馬

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; //點(diǎn)擊次數(shù)
        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("危險(xiǎn)提示:{0}你小子別狂點(diǎn),仗著{1}歲手速快是吧!?",e.Name,e.Age);
            MessageBox.Show(tool);
        }

    

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            ClickNuM++;
            if (ClickNuM>5)
            {
                //觸發(fā)自定義事件
                this.TestEventrg?.Invoke(this,new TestEventrgs("ming",17));//輸入的參數(shù)可以自己傳入
                ClickNuM = 0;
            }
        }

    }
}

放到界面上,狂點(diǎn)之后

在這里插入圖片描述

接下來是winfrom下的已有控件,以button為例子

先添加一個(gè)組件

在這里插入圖片描述

改為繼承 Button,并添加相應(yīng)的自定義事件

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();
        }
    }
}

將組件從工具箱添加到界面,添加對(duì)應(yīng)方法

在這里插入圖片描述

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("危險(xiǎn)提示:{0}你小子別狂點(diǎn),仗著{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;
            }
        }
    }
}

運(yùn)行之后,狂點(diǎn)。觸發(fā)

在這里插入圖片描述

到此這篇關(guān)于C#為控件添加自定義事件及自定義觸發(fā)的文章就介紹到這了,更多相關(guān)C# 控件添加自定義事件及觸發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論