Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add Custom event and Custom trigger for C # Control

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "C# how to add custom events and custom triggers for the control". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "C# how to add custom events and custom triggers to the control" can help you solve your doubts, following the editor's ideas slowly in depth, together to learn new knowledge.

Let's start with a random incident.

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

There are two types: custom controls and existing controls under winfrom

Let's start with a custom control.

Make a random interface.

Get on the horse

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; / / number of clicks public event EventHandler TestEventrg;// custom event public UserControl1 () {InitializeComponent () This.TestEventrg + = new EventHandler (DangeTip); / / Custom event binding method} private void DangeTip (object sender, TestEventrgs e) {string tool = string.Format ("Hazard Tip: {0} Don't be crazy, rely on {1} hand speed, right?", e.NameMage.Age); MessageBox.Show (tool) } protected override void OnClick (EventArgs e) {base.OnClick (e); ClickNuM++; if (ClickNuM > 5) {/ / trigger custom event this.TestEventrg?.Invoke (this,new TestEventrgs ("ming", 17)); / / input parameters can be passed in ClickNuM = 0 }

Put it on the interface, after you go crazy.

Next is the existing controls under winfrom, taking button as an example

Add a component first

Inherit Button instead and add corresponding custom events

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 TestEventrg; public MyButton (IContainer container) {container.Add (this) InitializeComponent ();}

Add components from the toolbox to the interface and add corresponding methods

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 ("danger Tip: {0} Don't be crazy, relying on the speed of {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;}

After running, go crazy. Trigger

After reading this, the article "how to add custom events and custom triggers for controls" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report