Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
136
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

BUILDING DYNAMIC FORMS AT RUNTIME 239

Creating Event Handlers at Runtime

You’ve seen 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. Obviously, this isn’t a technique you’ll be using every day; you can come back and read this section when the need arises.

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. Let’s say 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 could be anything; you don’t have to make up a name that includes the control’s or the event’s name.

Once 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:

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

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 will 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 that the user knows which box has the focus at any time. We’ll use the same handlers for all TextBox controls, and the code of the two handlers are shown in Listing 5.18.

Listing 5.18: 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 event handlers use the sender argument to find out which TextBox control received or lost the focus, and they set the appropriate control’s background color (property BackColor). We write one handler per event and associate it with any number of controls added dynamically. Technically, the

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com