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)
Code language: VB.NET (vbnet)
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)
Code language: VB.NET (vbnet)
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
Code language: VB.NET (vbnet)
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)
Code language: VB.NET (vbnet)
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.