網站首頁 編程語言 正文
先隨便搞個事件吧
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;
}
}
}
}
運行之后,狂點。觸發
原文鏈接:https://blog.csdn.net/weixin_39448579/article/details/124444448
相關推薦
- 2022-06-08 報錯:No fallback instance of type class**解決辦法
- 2022-05-18 Python3的正則表達式詳解_python
- 2022-12-10 Qt如何自定義滑動條_C 語言
- 2023-02-09 go?sync?Once實現原理示例解析_Golang
- 2022-06-07 Python?urllib庫的使用指南詳解_python
- 2022-04-04 asp.net使用原生控件實現自定義列導出功能的方法_實用技巧
- 2022-03-27 pygame實現貪吃蛇游戲_python
- 2022-06-09 ASP.NET?Core使用EF創建模型(索引、備用鍵、繼承、支持字段)_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支