Handling Events


Until this point, this chapter has purposely glossed over how event handling works in Visual Basic .NET. But now that you know how to create your own controls, you need to understand what Visual Basic .NET is doing whenever it creates some event-handling code for you. The following sections help you understand this.

Signatures of Event Handlers

In virtually all the program examples in this book so far, there has been an Exit button, the code for which looks like this:

 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _               System.EventArgs) Handles btnExit.Click  Me.Dispose() End Sub 

By default, the event Sub procedure is created by using the Private access specifier . By convention, standard events are named with the control name , followed by an underscore character, followed by the event name:

  NameOfControl  _  EventName  

This is also called the method name of the event. For an Exit button, this would be the Click() event method name:

 BtnExit_Click 

The method name is always followed by an opening parenthesis, a sender object, the arguments for the event, a closing parenthesis, the keyword Handles , the control name, the dot operator, and finally the event name. Collectively, these parts of the statement define the signature of the event. For example, this is the signature for the BtnExit_Click() event:

 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _               System.EventArgs) Handles btnExit.Click 

All the standard controls provided by Visual Basic .NET have predefined event handlers. For example, consider the btnExit button as presented in Listing 21.3. While you're in the Code window, you should select the btnExit button from the drop-down list of controls and then click in the Declarations section. You should then see a list of predefined event handlers for the Button object, as shown in Figure 21.13.

Figure 21.13. Predefined event handlers for a Visual Basic .NET Button object.

graphics/21fig13.jpg

If you click the plus sign for the line in Figure 21.13 that reads Windows Form Designer generated code and plow through the code you find there, you should eventually locate this line:

 Friend WithEvents btnExit As System.Windows.Forms.Button 

This statement tells Visual Basic .NET that btnExit is a Button object. The WithEvents keyword tells Visual Basic .NET that btnExit can generate, or raise, Button object “type events. The drop-down list shown in Figure 21.13 is a list of the events that a Button object can raise.

If you click one of the events shown in the drop-down list, Visual Basic .NET automatically generates the skeleton code for that event. For example, if you click the BackColorChanged option, Visual Basic .NET generates the following statement stub:

 Private Sub btnExit_BackColorChanged(ByVal sender As Object, ByVal e As _              System.EventArgs) Handles btnExit.BackColorChanged End Sub 

You can add whatever code you want to execute if the BackColorChanged() event is fired while the program runs.

Senders, Delegates, and Event Handlers

The button object is often called the sender in event processing. In other words, when you click the Exit button, the program sends a Click() event message to Visual Basic .NET, saying: "Hey, Visual Basic .NET! The user just clicked on my btnExit button." Visual Basic .NET then says to itself: "Oh great. Who is responsible for handling the btnExit object's Click() event?" Visual Basic .NET then looks through its list of event delegates. A delegate is simply another class created by Visual Basic .NET that holds references to the events that it can process. A delegate reference is really a pointer to the procedure that is assigned to handle the event. For example, Visual Basic .NET looks though its list of delegates until it finds the signature that says: Handles btnExit.Click . Visual Basic .NET then transfers control to that event handler.

As you have probably already guessed, an event handler is simply the code that processes an event. In your program, this procedure is the event that handles the btnExit object's Click() event:

 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As _               System.EventArgs) Handles btnExit.Click  Me.Dispose() End Sub 

The Handles btnExit.Click at the end of the first statement line is a pretty good clue as to which event is to be processed by this code. When the event handler handles this event, the program simply releases the form and ends the program.

Although this is a pretty slimmed-down explanation, you should be able to decipher what an event handler does and what an event is. For additional information on event handling, you can investigate the online help system.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net