Visual Basic.NET 2008 > Working with Forms

Creating Event Handlers at Runtime - Visual Basic.NET 2008

<< Previous Page Next Page >>

You saw how to add controls on your forms at runtime and how to access the properties of these controls from within your code. In many situations, this is all you need: a way to access the properties of the controls (the text on a TextBox control or the status of a CheckBox or RadioButton control). What good is a Button control, however, if it can’t react to the Click event? The only problem with the controls you add to the Controls collection at runtime is that they don’t react to events. It’s possible, though, to create event handlers at runtime, and this is what you’ll learn in this section.

To create an event handler at runtime, create a subroutine that accepts two arguments — the usual sender and e arguments — and enter the code you want to execute when a specific control receives a specific event. The type of the e argument must match the definition of the second argument of the event for which you want to create a handler. Let’s say that you want to add one or more buttons at runtime on your form, and these buttons should react to the Click event. Create the ButtonClick() subroutine and enter the appropriate code in it. The name of the subroutine can be anything; you don’t have to make up a name that includes the control’s or the event’s name.

After the subroutine is in place, you must connect it to an event of a specific control. The ButtonClick() subroutine, for example, must be connected to the Click event of a Button control. The statement that connects a control’s event to a specific event handler is the AddHandler statement, whose syntax is as follows:

AddHandler control.event, New System.EventHandler(AddressOf ButtonClick)

For example, to connect the ProcessNow() subroutine to the Click event of the Calculate button, use the following statement:

AddHandler Calculate.Click, New System.EventHandler(AddressOf ProcessNow)

Let’s add a little more complexity to the DynamicForm application. We’ll program the Enter and Leave events of the TextBox controls added at runtime through the Me.Controls.Add method. When a TextBox control receives the focus, we’ll change its background color to a light yellow, and when it loses the focus, we’ll restore the background to white, so the user knows which box has the focus at any time. We’ll use the same handlers for all TextBox controls. (The code of the two handlers is shown in Listing 5.10.)

Listing 5.10: Event Handlers Added at Runtime

Private Sub TBox Enter(ByVal sender As Object, ByVal e As System.EventArgs)
CType(sender, TextBox).BackColor = color.LightCoral
End Sub

Private Sub TBox Leave(ByVal sender As Object, ByVal e As System.EventArgs)
CType(sender, TextBox).BackColor = color.White
End Sub

The two subroutines use the sender argument to find out which TextBox control received or lost the focus, and they set the appropriate control’s background color. (These subroutines are not event handlers yet, because they’re not followed by the Handles keyword— at least, not before we associate them with an actual control and a specific event.) This process is done in the same segment of code that sets the properties of the controls we create dynamically at runtime. After adding the control to the Me.Controls collection, call the following statements to connect the new control’s Enter and Leave events to the appropriate handlers:

AddHandler TB.Enter, New System.EventHandler(AddressOf TBox Enter)
AddHandler TB.Leave, New System.EventHandler(AddressOf TBox Leave)

Run the DynamicForm application and see how the TextBox controls handle the focus-related events. With a few statements and a couple of subroutines, we were able to create event handlers at runtime from within our code.

<< Previous Page Next Page >>

Table of Contents

  1. Working with Forms in Visual Basic.NET
  2. VB Forms - Forms in Visual Basic.NET - The Appearance of Forms
    1. Properties of the Form Object in VB.NET
    2. Placing Controls on Forms in VB.NET
    3. Setting the TabIndex Property of the controls in a Form in Visual Basic.NET
    4. An Example Contacts Project using Forms in Visual Basic.NET
  3. Anchoring and Docking of Control in a Form in Visual Basic.NET
  4. Splitting Forms into Multiple Panes - Visual Basic.NET
  5. The Form’s Events - Visual basic.NET
  6. Loading and Showing Forms - Visual Basic.NET
    1. The Startup Form - Visual Basic.NET
    2. Controlling One Form from within Another - Visual Basic.NET
  7. Forms versus Dialog Boxes - Visual Basic.NET
    1. The MultipleForms Example - Forms in VB.NET 2008
  8. Building Dynamic Forms at Runtime - VB.NET 2008
    1. The Form’s Controls Collection - Visual Basic 2008
    2. Creating Event Handlers at Runtime - Forms in Visual Basic.NET
  9. Designing Menus, The Menu Editor in Visual Basic.NET
  10. The ToolStripMenuItem Properties - Visual Basic.NET
  11. Manipulating Menus at Runtime - VB.NET 2008
  12. Iterating a Menu’s Items - Vb.NET 2008

     
 
W3computing.com Copyright 2011 All Rights Reserved
 
  Home | Useful links | Contact us | Privacy Policy