I see the button event

xiaoxiao2021-04-04  267

I see the button event

Author

:

Jim

time

:

2006-6-21

Key words

: Virtual function (

Virtual

),cover(

Override

),event(

Event

),proxy(

Delegate

)

introduction

: In the object-oriented programming, the key knowledge points mainly involved are several listed above. During the learning process, it is often dispersed to learn, and there may be a phenomenon after school, this article mainly passed

C #

One button (

Button

The instance of the inheritance is explained to understand the respective usage and internal contacts. Since I have limited knowledge, if there is an error, I also hope that prawn is correct. The younger brother will thank you here.

J

Foreword

: I believe that you must use the button in the system development process (

Button

). In the process of use, it may be dragged from the toolbox.

Button

In the form, double-click the button, automatically write the button

click

Event code, you can use the button to use the power of the blow, how is he working in the inside of the system? I believe that in-depth discussion and research may be less, :) The following is the younger brother, after the shallow research, the inspiration is now sorted out to give you a reference to reach the role of throwing jade.

initial analysis

: When we drag a button from the toolbox to our form, actually in the form

Designer.cs

In the middle, you will first have a reference to this button:

Private

System.windows.Forms.Button Sysbtn;

Sysbtn

its mine

Button

Name, please pay attention to this time

Sysbtn

Type Description, will be involved in the following description.

When we need to make a click event to be encoded, we only need to double-click this button to automatically appear for us to fill in.

Private

Void sysbtn_click (Object Sender, Eventargs E)

{

Messagebox.show ("

This is the event commission of the system button "

);

}

In fact, in the form

Designer.cs

In the middle, the specific definition and description of the event will be automatically generated.

THIS

.sbtn.click = new system.eventhandler (this.sysbtn_click);

Now explain and explain this line of text:

The first is the event:

this.sysbtn.click

,we know

Click

Yes

Button

An event, he is just

Button

Class

N

One of an event, but they all have the same nature and usage, ie: Events are triggered by class of definition events and implement specific event processing outside the class.

Then it is a proxy (

Delegate

)

The agent is actually a pointer function, he specifies the prototype definition of the event, such as the name, proxy parameters, for the above

Button

of

click

The agent of the event has been

C #

Compiling system predefined,

System.EventHandler

(

Eventargs E

), The agent actually played a bridge, through the agent, connect the interior event with the external processing of the class.

Finally, external event processing:

In this example, external processing is: private

Void sysbtn_click (Object Sender, Eventargs E)

{

Messagebox.show ("

This is the event commission of the system button "

);

}

The external processing function prototype must be consistent with the agent. When you click the button, it will be displayed.

"

This is the event commission of the system button

"

Message Box.

In-depth analysis:

Now we go deep into

Button

Inside, study again

click

The working principle of the event, through the previous description, we understand that the event is triggered inside the class, but what is the specific thing triggered?

We pass

MSDN

Help can understand

Button

The composition of all members of the class will find some

protected

Virtual Void Onclick (Eventargs E)

Function, from the function name we can see him

Click

The incident has an inevitable connection, in fact, the Yuan fierce is him, he triggered him.

Button

of

click

Incident, we can verify the following examples. Let's take a specific analysis:

The first is the virtual function

Virtual

,

The main feature of the virtual function is to be overwritten by inheritance. In the following description, we will find his mystery.

Then we pass inheritance

Button

Class, build our own

Button

class

MyBTN

Class

MyBTN: System.Windows.Forms.Button

{

}

And quote our in the form

Button

Class, and event definitions, and implementation functions

Private

Mybtn mybtn;

THIS

.mybtn.click = new system.eventhandler (this.mybtn_click);

Private

Void MyBTN_Click (Object Sender, Eventargs E)

{

Messagebox.show ("

This is my button event entrusted "

);

}

Obviously, it will also be displayed when the button is clicked.

"

This is my button event commission

"

Message Box.

Next, in order to discover

protected

Virtual Void Onclick (Eventargs E)

The mystery, we are

MyBTN

Class over this function and implement display

"MyBTN

It is inherited from System.Windows.Forms.Button to trigger the Click event, but the system agent has expired.

Class

MyBTN: System.Windows.Forms.Button

{

Protected Override Void Onclick (Eventargs E)

{

Messagebox.show ("MyBTN

It is inherited from System.Windows.Forms.Button to trigger the Click event, but the system agent has been invalid. "

);

}

}

Now let's click our button, guess what happens, what happens, is it a few messages?

0

? One? Two?

The answer is that only a message box is displayed.

"MyBTN

It is inherited from System.Windows.Forms.Button, which triggers the Click event, but the system agent has been invalid. "He did not trigger the message processing process that we externally defined:

Private

Void MyBTN_Click (Object Sender, Eventargs E)

{

Messagebox.show ("

This is my button event entrusted "

);

}

From this, we can find that

Button

of

click

Event is

Onclick

The function is triggered, and we will generate a problem when inherited.

Button

After the class, how can we let him trigger?

click

Incident?

Very simple, we only need to cover us

Onclick

The function in the function is called in the base class.

Onclick

Function can be.

Class

MyBTN: System.Windows.Forms.Button

{

Protected Override Void Onclick (Eventargs E)

{

Messagebox.show ("MyBTN

It is inherited from System.Windows.Forms.Button to trigger the Click event, but the system agent has been invalid. "

);

Base.onclick (e);

}

}

all

OK

!

Be based on the base class

Onclick

Function to trigger

click

Event, as for the base class

Onclick

How is it triggered, then you have to ask Microsoft.

J

to sum up

: Through the above examples we can summarize the event: an event of the class is to trigger in its internal trigger, often triggered by a virtual function. Class events are associated with external processing functions, by users to implement their own event processing.

what

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

New Post(0)