Henry's VB.NET Tour (12) - Event Drive

zhaozj2021-02-16  50

Henry's VB.NET Tour (12) - Event Drive

Han Rui

"It is so convenient to inherit it!" I silently sigh my heart. After a while, after I looked up, I suddenly found a new problem after I saw the code on the screen, and the button clicked event program has now become:

VB.NET program

VB program

Private sub btnprotected_click (Byval e as system.EventArgs) Handles btnprotaced.click

MSGBOX ("Delivery Protection Button")

End Sub

Private submmand1_click ()

......

End Sub

The original VB program is so simple. What is the current parameters Sender and E? What is the Handles? It's also good to have a teacher, I immediately got up, I'm holding big Li, please tell him questions one by one.

Da Li pushed the eye mirror and asked me: "You should be familiar with the event driver design in Windows program?" (Note: Windows here refers to Win9X and above operating systems)

"Yes!" It seems that it is the opening word of Da Li, but I have to jump in his circle. "The event driver is to say that the execution process of the application is determined by the incident occurred by the outside world. It is a mode that accepted the task. The event is a signal. It tells the application to have important cases. In fact, the implementation is that each application will be sent to the Windows operating system during its operation. Let these objects waits for events generated by Windows, and then processes. "

"VB programmers generally only need to understand like this." Big Li really let me touch, "It should be said that Windows first generates messages, the window program in the application will receive messages from Windows, and will It turns into events, this will then say later. Now let's take a look at the composition of the event driver, mainly there are three elements of events, objects, and event handlers. Object is the main body of the task, such as what you said button1; incident, It is the task to be executed. For example, click, it is a Click event; then the event handler is the Button1_Click program. "

"I know this! I just want to ask Sender ..." I responded to the road.

Big Li Ge shook his hand and interrupted my words. "If you really know the event-driven, you will understand. You look, what type of variables are Sender?"

"Object!" I asked helplessly, "But, ..." I felt what.

Da Li smiled and said, "Sender As Object, the source and use of it. Object is support all classes in the .NET Framework class hierarchy, and provides low-level services for derived classes. This is .NET frame All kinds of ultimate superclars; it is the root of the type hierarchy. In general, Sender indicates the source of the incident in the shape, which is the 'object of the three elements I have just mentioned. If the event triggered in the control If you write code, you don't need to reassign it, because it has default that it is the control. When you write a code to call an event program, you will indicate what Sender is. "

"That is to say, is Sender to be provided to the internal or external or external to the event handler code?" I still don't understand.

"It can be said, don't you see that VB.NET is provided to us is more comprehensive, more direct control? Say e, indicating event data, is an event to inspire the status information. When the event is triggered Events that do not pass status information to event handlers will set E Eventargs. If the event handler requires status information, the application must be born in this class to save data. For example, the mousedown event, the system needs to judge the position of mouse, judgment It is the key to the left and right right click, and it is necessary to click a few times, so the e must be an instance of the System.Windows.Forms.MouseEventArgs class. "Da Li continued to explain. "Oh, that is, e is closely related to the event?" I started to confirm with the actual operation, I chose Button1 in the "Class Name" drop-down list in the code window, and pull it on the right "method name" drop-down The MOSUEDOWN method is selected in the list, which is the Button1.MouseDown event handler code segment:

Private sub button1_mousedown (byvale) (byval e as system.windows.forms.mouseeventargs) Handles Button1.MouseDown

End Sub

Sure enough, you see the type of E changes to System.Windows.Forms.MouseEventArgs. I wrote E, then hit a point after it, appeared in the next point (as shown in Figure 1):

Compared to Button1.Click events, the properties and methods of E in the Button1.Mosuedown event have five properties that describe event feature, as described in Table 1.

Button

Get which mouse button that has been pressed.

ClickS

Gets the number of times the mouse button is pressed and released.

Delta

The signed count of the number of brakes whose mouse wheel has been rotated. The brake is a recess of a mouse wheel.

X

Get the X coordinate of the mouse click.

Y

Get the y coordinate of the mouse click.

Table 1 Attributes of E of E

"I understand, then is the Handles keyword is the process of declaring the code is to handle which event is handled?" I finally opened.

"Yes, don't look at Handles, take advantage of it, your program will become simple and flexible." Big Li wrote a code for me:

Private sub button1_click (Byval Sender As System.Object, Byval E as System.EventArgs)

Handles Button1.click, Button2.click

Select Case Sender.name

Case "Button1"

Msgbox ("You click Button1")

Case "Button2"

Msgbox ("You click Button2")

End SELECT

End Sub

"The declarations of two events have been written later in Handles, and two objects can be controlled in the same segment. In addition, we can also build and call your own event handler more easily." Da Li Say it while writing.

"Yes? How to achieve?" I couldn't help but have been excited.

(Endlessly)

-------------------------------------------------- -------------

Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.

QQ: 18349592

E-mail: Henry7685@hotmail.com

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

New Post(0)