Understanding of agents and events in C # (2)

Source: Internet
Author: User

Understanding of agents and events in C # (2)

Hi! Hello everyone, do you still remember the article about proxy? Do you want to ask why I want to write separately?

The reason is very simple. They said they needed more than seven articles to become special writers. Oh, my God, this is actually my ninth article.

However, the two articles are still under review. Joking, in fact, there is a connection between the structure, proxy and the event, but it is also possible.

This is why I want to separate them, because many of my friends used proxies and events at the beginning.

Is used to add events to a button or form.

When people mention proxy and event, they will naturally think of Windows Time-driven. Yes, that is the most commonly used, but in fact

It can also be used separately. At least the proxy is like that. The proxy can survive independently from the event.

As you can see. Okay, let's get into the right corner.

Event:

It is not difficult to understand events. In fact, every person experiences one or another events every day.

Life. Think about it. A weather forecaster tells you that it will rain tomorrow. Naturally, you will think that you should take the umbrella on your behalf.

This is the event. The weather forecast sends a notification of this event and you will handle this event. What you do is to take the umbrella to prevent rain.

As you often do, you click a button and then send a click event. The next step is to handle this event, maybe

A pop-up window or anything may happen, or even shut down the computer. These are all user events.

The GUI is triggered and then these events occur.

Next let's take a look at how C # deals with it. So I mentioned in the previous article, turning a verb into a noun. What do you think?

Is it + ing? Haha. Make a joke. Of course not. We are not making an English contact. Okay, it's a proxy, just as you often do.

In the IDE environment, drag a button to the form and double-click it. What do you see? Have you entered the code interface? Then you

To handle the event. But what does this have to do with the proxy? Well, why don't you look for it carefully?

This. button1.click + = new system. eventhandler (this. button#click );
In the initializecomponent () function. Yes, this is the proxy. It puts the event into the proxy. What about the event?

In this. button#click method, you will see that it has two parameters, one of which is an object-type sender. This is very simple.

As the name suggests, it is the initiator of the event. Who is it? You only need to ask it to know. What about the second one? It's an event, right?

Of course, the initiator brought his event, just as the weather reporter brought his weather forecast. Okay, believe me.

One day, humans can make weather. Listen, believe me, it's raining and so on.

One day, we will create new weather effects. For example, how about making money? Haha. It sounds like a good idea, although it is

A joke. Okay, you'll say I'm crazy. Why do you say that? No, I want to talk about a new topic, that is, user-defined

Events and system predefined events. We will discuss these two events respectively.

Custom events:

I believe that the Code will always be the most convincing, just as if you don't want to watch teaching materials and want to watch science and education movies.

The following code describes the facts.

 

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;

Namespace userevent
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public Delegate void myshow ();

Public class form1: system. Windows. Forms. Form
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Public event myshow showme;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();

//
// Todo: add Any constructor code after initializecomponent calls
//
This. showme ();
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region windows Form Designer generated code
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (292,273 );
This. Name = "form1 ";
This. Text = "form1 ";
This. showme + = new myshow (this. showmename );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
Void showmename ()
{
MessageBox. Show ("I'm yarshray ");
}
}
}

Have you seen the above Code? You can also guess what I want to talk about, depending on a custom proxy.

Public Delegate void myshow (); this proxy will represent an event, which is defined here

Public event myshow showme; the name of the event I defined is showme.

This is to show me, huh, huh.... Let's take a look at this. showme + = new myshow (this. showmename );

I used this event and handed the showmename to him. This method is very simple, that is, a dialog box is displayed. Write me

The name is "yarshray", but where is this event triggered? Why not look up? Have you seen the constructor?

Yes, there is a such. showme (); in it, but you know, it doesn't make any sense to do this. The actual situation is that you need to customize it.

The message is then dropped into the message loop, and the event is triggered through the message. However, I am only talking about the agent and event, so

This is not the case. Just for the sake of simplicity, you can see more clearly. Well, I will talk about user-defined events here. The following

Let's talk about the predefined events.

 

Predefined events:

In fact, I don't need to talk much about it. Everyone has used it. As I said at the beginning, the weather forecaster will take his weather forecast with him.

That's all God's pre-arranged, Windy, rainy, Snowy, hail, etc. Just use it directly. Just like a button.

Click. enter with the mouse, move the mouse away, and so on. I will not give the example. Anyone has used it,

Well, I have finished writing it. I hope it will be helpful to you. Of course, it looks more like a bunch of nonsense, right? Well, you just think of it as

In the end, I would like to thank you for reading this article. I will write it again. Let's go back to it.

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.