The most important thing in Windows programming is the control event, and Microsoft gives us a good way to focus on the design and coding of the event execution method, but we really understand how the event is actually going to work, and the improvements to our programming are really great, like in Windows programming If I clicked a button that triggered the button's Click event Button1_Click () {}, but sometimes when we program, not only do we want to trigger a button clicking event, I also want to put the other time also to call down order execution, to achieve this way , in addition to the invocation of other methods at the end of the method, it is possible to encapsulate other methods that need to be executed sequentially into the delegate object of the button's click event, so that the programs in the list of methods of destruction can be executed sequentially, and the implementation is premised on a clear event trigger and a delegate invocation.
Events are messages that are sent to the outside world by classes and objects, and the execution of events is done through event delegation, by invoking the processing method we have prepared, but by responding to the message first. To respond to certain events and to implement our intended approach for certain events, you need to do the following steps:
1, declare the event delegate.
2, declare the event.
3. Add the trigger method of the event.
4. Add handlers for events (methods to respond to events).
5. The specified event handler is set to the event to be processed (subscription event).
6, the user information operation, and trigger the event (Invoke Event Trigger method).
7. Execute the event handler we need through the callback of the event delegate.
Let's give an example of a simple custom event handler (console program)
The code is as follows:
Namespace events
{
The class that publishes the event
public class Testeventsource
{
Defining event argument Classes
public class Testeventargs:eventargs
{
Public ReadOnly char Keytoraiseevent;
Public Testeventargs (Char keytoraiseevent)
{
Keytoraiseevent = keytoraiseevent;
}
}
Define Delegate
public delegate void Testeventhandler (object sender, Testeventargs e);
Declaring an event object with the event keyword
public event Testeventhandler TestEvent;
Event Triggering method
protected virtual void Ontestevent (Testeventargs e)
{
if (testevent! = null)
TestEvent (this, e);
}
Raising an Event
public void RaiseEvent (char keytoraiseevent)
{
Testeventargs e = new Testeventargs (keytoraiseevent);
Ontestevent (e);
}
}
class to listen for events
public class Testeventlistener
{
Defines the method that handles the event, which has the same parameters and return value type as the delegate that declares the event
public void keypressed (object sender, Testeventsource.testeventargs e)
{
Console.WriteLine ("Sender: {0}, as per health: {1}", sender, E.keytoraiseevent);
}
Subscribe to Events
public void Subscribe (Testeventsource evensource)
{
Evensource.testevent + = new Testeventsource.testeventhandler (keypressed);
}
Unsubscribe Events
public void Unsubscribe (Testeventsource evensource)
{
Evensource.testevent-= new Testeventsource.testeventhandler (keypressed);
}
}
Test class
public class Test
{
public static void Main ()
{
Creating an Event source object
Testeventsource es = new Testeventsource ();
Creating Listener Objects
Testeventlistener el = new Testeventlistener ();
Subscribe to Events
Console.WriteLine ("Subscription event \ n");
El. Subscribe (es);
Raising an Event
Console.WriteLine ("Enter a character and press Enter");
string s = Console.ReadLine ();
Es. RaiseEvent (S.tochararray () [0]);
Unsubscribe Events
Console.WriteLine ("\ nthe unsubscribe event \ n");
El. Unsubscribe (es);
Raising an Event
Console.WriteLine ("Enter a character and press Enter");
s = Console.ReadLine ();
Es. RaiseEvent (S.tochararray () [0]);
}
}
}
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Custom events and Delegate instances in C #
This address: http://www.paobuke.com/develop/c-develop/pbk23211.html
Related Content C # WinForm Implementing a Form control free drag function Example C # network crawler code share C # Simple crawl tool analysis of the principle of the LZW data compression algorithm C # Implementation of file breakpoint continuation download method
C # How to monitor folder changes C # Implement methods to improve the speed of XML reading and writing DevExpress implement the method of setting watermark text for TextEdit C # Network Programming basics Process and threading
Custom events and Delegate instances in C #