Simple Example: quickly learn about event processing and delegate event delegate and eventdelegate
The following is just the simplest way to represent events. In actual application, some operations may be notified to each other between different forms to trigger events.
First, declare one or more EventHandler parameters for a degate, but the trigger and use must match.
Create an EvenHandler instance
Generates an event trigger statement when the program is established or you need it.
[Csharp]View plaincopy
- A + = new EventHandler (d );
Public delegate void EventHandler (string s );
Actually triggered event ("")
You can call the actual operation.
[Csharp]View plaincopy
[Csharp]View plaincopy
- Public event EventHandler;
- Public Form1 ()
- {
- InitializeComponent ();
- // Trigger the Declaration event
- A + = new EventHandler (d );
- }
- Private void button#click (object sender, EventArgs e)
- {
- MessageBox. Show ("an event is about to be triggered! ");
- // Trigger
- A ("ss ");
- MessageBox. Show ("11 ");
- }
- // Implementation
- Public void d (string s)
- {
- // System. Threading. Thread. Sleep (10000 );
- MessageBox. Show ("d." + s );
- }
Simple implementation of delegate and event
Using System;
Using System. Windows. Forms;
Namespace delegetssss
{
Public partial class Form1: Form
{
Public delegate void ButtonClickEventHandler (object sender, EventArgs e );
Private event ButtonClickEventHandler ClickOverTen;
Private int clickCount = 0;
Public Form1 ()
{
InitializeComponent ();
This. ClickOverTen + = new ButtonClickEventHandler (Button_ClickOverTen );
}
Private void button#click (object sender, EventArgs e)
{
This. clickCount ++;
If (clickCount> = 10)
ClickOverTen (this, e );
}
Private void Button_ClickOverTen (object sender, EventArgs e)
{
MessageBox. Show ("You have clicked the button ten times !! ");
This. clickCount = 0;
}
}
}
What are the differences between delegate and EventHandler?
The standard signature entrusted by the event handler defines a method without return values. The first parameter type is Object, which references the instance that triggers the event. The second parameter is derived from the EventArgs type, it stores event data. If the event does not generate event data, the second parameter is only an instance of EventArgs. Otherwise, the second parameter is a custom type derived from EventArgs and provides all the fields or attributes required to save event data. EventHandler is a predefined delegate dedicated to event handler methods that represent events that do not generate data. If an event generates data, you must provide your own Custom Event Data Type and create a delegate. The second parameter type is custom, you can either use the generic EventHandler delegate class and use a custom type to replace the generic type parameter. To associate an event with the event handling method, add a delegated instance to the event. Unless the delegate is removed, the event handler is called whenever this event occurs.
Public delegate void EventHandler (Object sender, EventArgs e); public event EventHandler NoDataEventHandler;
A delegate Declaration defines a type, which uses a specific set of parameters and the return type encapsulation method. For static methods, the delegate object encapsulates the methods to be called. For instance methods, the delegate object encapsulates an instance and a method on the instance at the same time. If you have a delegate object and a set of appropriate parameters, you can use these parameters to call the delegate.