Understand the proxy and events in C # (2)

zhaozj2021-02-16  44

Understand the proxy and events in C # (2)

Hey! Hello, do you remember the on behalf of the agent? Do you want to ask me to write?

The reason is very simple they say that there are more than 7 or more, it can be a special writer. Oh, God, in fact this is my ninth article

But that two. It is still in the review. In fact, the structure, the agent and the incident are connected, but it is

Separate two topics discussed. This is why I want to separate, because many friends use the proxy and events at first

When it is used to add an event to a Button or a form. So you can often see, there is

One person will naturally think of the time driver of Windows. Yes is the most commonly used, but in fact

It is also possible to use separate, at least the agent is that. The agent can leave the event independently survive. The one above

As you can see. Ok, let me say, let us enter the topic.

event:

Understand incident, this is not difficult, in fact, everyone is experiencing such or such incidents every day, then consisting, you

Life. Think about it, a weather forecaster informs you to say tomorrow. It is natural, you will think of it, you should take an umbrella. Yes

This is the event. Weather forecast issues this event notification, then you will handle this event. What you do is to bring your umbrella to rain.

Just like you often do it, you click on a Button and issue a Click event, which is of course handling this incident, perhaps

Popping a window, or something else can happen, or even shut down the computer. These are user events. Because users

Trigger the GUI and then these events.

Let's take a look at how it is handled in C #. Then I mentioned in front of the article, turning the verbs into nouns. What would you think?

Is it ing? Haha .. Opening a joke, of course, we are not in English. Well, it is a proxy, just like you often do.

In the IDE environment, you drag a button to the form, then double-click it, what do you see? Do you enter the code interface? Then you will

To handle events. But what is the relationship between this and the agent? Well, why don't you find it carefully?

This.button1.click = new system.eventhandler (this.button1_click); a sentence in your initializeComponent () function. Yes, this is the agent. It puts the event in the agent. So what?

In this.Button1_Click method, you will see it has two parameters, one is the object type Sender, this is simple

As the name suggests it is the initiator of the event. Who is it, you just ask it. So the second? Is it an event, right?

Of course, the initiator is coming with his incident, just like the weather forecast with his weather forecast. Ok, I believe in me.

Humans have a day to make weather. Listening to me, I have to make a long-term weather, etc.

We will always create new weather effects, how is it? Hehe .. It is a good idea, although it is

A joke. Ok, you will say that I am going crazy, why don't you say this? No, I want to talk about a new topic. That is user-defined

Events and system predefined events. Then let's discuss these two events separately.

User-defined event:

I believe that the code will always be most convincing. Just like you don't want to see the textbook, I will be more willing to see the science and teacher movie.

The following code is the fact that I want to explain.

Using system.drawing; using system.componentmodel; using system.windows.form; using system.data ;. using system.data ;. USING SYSTEM.DATA;

A summary description of Namespace useevent {///

/// form1. /// public delegate void myShow ();

Public class form1: system.windows.Forms.form {///

// The required designer variable. /// private system.componentmodel.container component = null; public event myshow showme; public form1 () {// // Windows Form Designer Support for // InitializeComponent (); //// // TODO: Add any constructor code to add any constructor code // this.showme ();}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose (4);

#Region Windows Form Designer Generated Code ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// 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

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());} void showmename () {MessageBox.show ("I'm Yarshray);}}}

See the code above? No need to say more, you also guessed what I want to say, see a proxy for my custom

Public delegate void myShow (); This agent will proxy an event, it is defined here.

As follows: public evenet myShow showme; I defined this event name called ShowMe, people like it

Just show me, huh, huh .. Take a look, one of the sentence this.showme = new myshow (this.showmename);

I used this incident and handed the showmename. This method is very simple is to pop up a dialog. Write me

The name "Yarshray", but this event triggered there? Why not look up? See the constructor?

Yes, there is this.showme (); but you know that there is no meaning, the actual situation is required to be customizable.

The message then fell it to the message loop, and then triggered the event through the message. But I just talk about the agent and the event, so

There is no so. Just for the details. Let you see more clearly. Ok, the user-defined event I will talk about it. The following time let us talk about the predefined event.

Predefined events:

In fact, I don't have to say more. Everyone used it. In the beginning, the weather forecast was the same as his weather forecast.

That is the pre-arrangement of God, rain, snow, hail, etc. Direct use. Just try to press New

Click. The mouse is entered, the mouse is removed, and so on. Example I didn't give it, no one used it,

Ok, I'm finished, I hope to help you, of course, it's more nonsense, is it? Well, you will be a kind

Come. Finally, thank you for reading. I will also write. In this way, I will see it.

转载请注明原文地址:https://www.9cbs.com/read-27448.html

New Post(0)