Creating Event Handlers in the Code Editor


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 8.  Adding Events

Creating Event Handlers in the Code Editor

At the top of the Code editor, there is an Objects drop-down list on the left and an Events drop-down listcalled Procedure list in VB6. The Objects and Procedures (or Events) controls existed in the Code editor in VB6, too (see Figure 8.3). To create an event handler in VB6 as well as Visual Basic .NET, select an object from the list on the left and the event from the list on the right. The Code editor generates the event procedure for you automatically.

Figure 8.3. Create an event handler in the Code editor by selecting the class and method names .


In VB6, the mechanism for wiring event procedures to events was implied . However, in VB 6, if you manually typed

 Private Sub Form_Load() End Sub 

the event procedure was automatically associated with the event. In VB6 it was simply a matter of naming the procedure correctly, but the mechanism for associating the event procedure with the object was implicit.

Visual Basic .NET allows you to create events the same wayselect an object and an event and the code is generated. Or, you can type the event procedure manually, but you have to associate the procedure with the event with code. Event procedures cannot be implicitly associated with the event simply by typing a correct procedure name . This added code requires a little more effort, but offers significantly greater advantage.

Visual Basic .NET allows you to associate event procedures with events in four advantageous ways: use the Form designer and create the event handler with the Properties view; use the Code editor's Objects and Procedures drop-down list; manually type the WithEvents statement and define the event procedure with a Handles clause; or define the procedure at design time and use the AddHandler method to associate the event handler at runtime. VB6 did not support creating events with the Properties view, nor did it support passing events as procedural type arguments. In the next two subsections, I will demonstrate how to write the WithEvents statement and the event procedure and how to invoke the event handler with code.

Writing Event Handlers in the Code Editor

You need to perform two related operations to define event handlers at design time. First, you need to define a WithEvents statement, and second, you need to define a procedure with the correct signature and add a Handles clause at the end of the procedure.

For the example, let's declare a WithEvents statement for a form and define a Click event handler. A Form class is defined in System.WinForms.Forms. The WithEvents statement is as follows :

 Private WithEvents Form1 As System.Winforms.Forms.Form 

Form1 is the object and System.WinForms.Forms.Form is the fully qualified path of the class. To define an event handler that automatically handles click events for Form1, we need a procedure with the correct signature and a Handles clause. The Click event handler is defined as an EventHandler delegate, a procedural type that is specifically a subroutine that has Objects and System.EventArgs arguments, so we need a procedure that takes those types of arguments:

 Private Sub Form1_Click(ByVal Sender As Object, _    ByVal e As System.EventArgs) Handles MyBase.Click End Sub 

Listing 8.8 demonstrates both parts of the manually typed Click event handler (the rest of the Form code is hidden using code outlining).

Listing 8.8 Manually defining event handlers.
  1:  Public Class Form1  2:  Inherits System.Windows.Forms.Form  3:  Private WithEvents Form1 As System.Windows.Forms.Form  4:   5:  [...]  6:   7:  Private Sub Form1_Click(ByVal Sender As Object, _  8:  ByVal e As System.EventArgs) Handles MyBase.Click  9:   10:  MsgBox("Manually Typed")  11:   12:  End Sub  13:   14:  End Class 

When you click the form having the code shown in Listing 8.8, the Form1_Click event handler on line 7 is called. The manually typed code is identical to the code generated by selecting (Base Class Events) from the Objects list and Click from the Procedures list.

Typing the code manually is not something you'll want to do all the time. However, it's necessary to understand how event handlers are wired together to use more advanced delegate techniques.

Associating Event Handlers at Runtime

Listing 8.8 defines a Click event handler. We could have named the Click event handler anything; I named it Form1_Click out of habit but ClickEvent would have worked. The Handles MyBase.Click clause is the part of the procedural statement that indicates that the procedure Form1_Click is an event handler.

Form1_Click is also a procedure that we can invoke directly and can be treated as a procedural type and assigned to an instance of a delegate. To invoke the procedure, simply call the procedure with two suitable arguments. Form1_Click(Nothing, Nothing) will call Form1_Click, passing Nothing as the value of the Objects and System.EventArgs parameters.

In Visual Basic .NET, you can assign the address of the procedure as the value of a Delegate variable:

 Dim ClickEvent As EventHandler = AddressOf Form1_Click ClickEvent(Nothing, Nothing) 

EventHandler is a predefined Delegate, as mentioned. The first statement of the fragment declares ClickEvent as an EventHandler delegate and assigns the address of Form1_Click to ClickEvent. The second statement invokes the procedure through the delegate. The second statement in the fragment calls the Form1_Click subroutine.


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222 © 2008-2017.
If you may any questions please contact us: