Henry's Journey to the VB.net (12)-event-driven

Source: Internet
Author: User
Henry's Journey to the VB.net (12)-event-driven

Han Rui



"The visual inheritance is so convenient!" My heart silently sighs. After a while, I glanced up at the code on the screen, and suddenly I found a new problem, and the button's Click event has now become:

Vb. NET Program
VB Program

Private Sub Btnprotected_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Btnprotected.click

MsgBox ("Derived protection button")

End Sub


Private Sub Command1_Click ()

......

End Sub






And the original VB program is so simple. Now what are the parameters sender and E for? What the hell is that handles? Fortunately there is a teacher, I immediately get up, thick face of big Lee Please come over and say to him all the questions to listen.

Big Lee pushed the glasses and asked me, "Are you familiar with the event-driven programming in Windows programming?" "(Note: Windows here refers to Win9x and the operating system above it)

"Yes!" said the man. "It seems that the rhetorical question is the opening word for Big Lee, but I have to jump into his trap first," event-driven means that the execution process of an application is determined by events that occur outside the world. That is, the mode of accepting a task to work. An event is a signal that tells the application that something important is going to happen. In practice, each application sends objects that are responsible for different tasks to the Windows operating system during its run, allowing them to wait for the events generated by Windows and then process them. ”

"VB programmers generally need just like you to understand the line." "Big Lee's words really touched me," it should be said that Windows first generated a message, the Windows Program window procedure can receive messages from windows and turn it into an event, which we'll talk about later. Now let's look at the composition of the event driver, which consists of three elements of event, object, and event handlers. The object is to complete the task of the subject, such as you say Button1, the event is the object to perform the task, such as clicks, is the click event, then the event handler is button1_click this program. ”

"I know that!" I just wanted to ask sender ... "I replied doubtfully."

Darico shook his wave and interrupted my words. "If you really know the event-driven, you get it." You see, what type of variable is sender? ”

"Object!" I asked helplessly, "but, ..." I vaguely felt something.

Big Li smiled and said, "sender as Object, it nail its origin and use." object is to support all classes in the. NET Framework class hierarchy and provide low-level services for derived classes. This is the ultimate superclass of all the classes in the. NET framework, which is the root of the type hierarchy. In general, sender the source of the event in the formal parameters, which is the ' object ' of the three elements I have just described. If you write code in an event that is raised by a control, you generally do not need to reassign it because it is already implicitly considered to be the control. When you write code to invoke an event program, you should indicate what sender is. ”

"In other words, is the sender provided to be invoked inside or outside the event handler code?" "I still don't understand a bit.

"Can you say that, do you not see that vb.net provides us with more comprehensive and direct control?" Again, E represents the event data, which is the state information needed to fire an event. An event that does not pass state information to an event handler when the event is raised will set E to EventArgs. If the event handler requires state information, the application must derive a class from this class to hold the data. For example, the MouseDown event, the system needs to determine the location of the mouse, the judge is left to the right key click, the judge clicked a few, so the E must be an instance of the System.Windows.Forms.MouseEventArgs class. Big Li continues to explain.

"Oh, that means E is closely related to the incident?" "I started with the actual operation to verify that I chose Button1 in the class name Drop-down list in the Code window, and the Mosuedown method was selected in the method name Drop-down list on the right, and the Button1.mousedown event handler code snippet appears:

Private Sub Button1_mousedown (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) Handles Button1.mou Sedown

End Sub




Sure enough, see the type of E into System.Windows.Forms.MouseEventArgs. I wrote down E in the code snippet, then hit a point behind it, and there was an IntelliSense menu (as shown in Figure 1):





Compared to the Button1.Click event, the properties and methods of E in the Button1.mosuedown event have five more properties that describe the event characteristics, as described in Table 1.

Button
Gets which mouse button was pressed.

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

Delta
Gets a signed count of the number of brakes that have been rotated by the mouse wheel. The brake is a notch of the mouse wheel.

X
Gets the x-coordinate of the mouse click.

Y
Gets the y-coordinate of the mouse click.



















Table 1 The properties of the extra E

"I see, then, is the handles keyword the process of declaring that the code is going to handle which event?" "I finally got a little bit of an opening.

"Yes, but don't underestimate handles, make good use of it, your program will become concise and flexible." "Big Li said while writing 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 clicked on Button1")

Case "Button2"

MsgBox ("You clicked on Button2")

End Select

End Sub






"After you write a two-event declaration, you can control two objects in the same piece of code," handles. In addition, using sender and E We can also more easily build and invoke our own event handlers. Big Li writes side to say.

"Is it?" he said. How does it happen? "I'm not motivated by a deep interest.

(To be continued)



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.