Simple Example: quickly learn about event processing and delegate event delegate and eventdelegate

Source: Internet
Author: User

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
  1. 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
  1. Public event EventHandler;
  2. Public Form1 ()
  3. {
  4. InitializeComponent ();
  5. // Trigger the Declaration event
  6. A + = new EventHandler (d );
  7. }
  8. Private void button#click (object sender, EventArgs e)
  9. {
  10. MessageBox. Show ("an event is about to be triggered! ");
  11. // Trigger
  12. A ("ss ");
  13. MessageBox. Show ("11 ");
  14. }
  15. // Implementation
  16. Public void d (string s)
  17. {
  18. // System. Threading. Thread. Sleep (10000 );
  19. MessageBox. Show ("d." + s );
  20. }

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.