CREATING CONTROLS

Usually you add controls to a form graphically at design time. In some cases, however, you may want to add new controls to a form while the program is running. This gives you a bit more flexibility so that you can change the program’s appearance at run time in response to the program’s needs or the user’s commands.

For example, you may not know how many pieces of data you will need to display until run time. Sometimes you can display unknown amounts of data using a list, grid, or other control that can hold a variable number of items, but other times you might like to display the data in a series of labels or text boxes. In cases such as these, you need to create new controls at run time.

The following code shows how a program might create a new Label control at run time. First it declares a variable of type Label and initializes it with the New keyword. It uses the label’s SetBounds method to position the label on the form and sets its Text property to “Hello World!” The code then adds the label to the current form’s Controls collection. (“Me” is a keyword that means “the object currently executing code,” which in this case is the form.)

Dim lbl As New Label
lbl.SetBounds(10, 50, 100, 25)
lbl.Text = "Hello World!"
Me.Controls.Add(lbl)

CHANGING CONTAINERS
To place a control inside a container other than the form, add the control to the container’s Controls collection. For example, to add the previous Label to a GroupBox named grpLabels, you would use the statement grpLabels.Controls.Add(lbl).

Usually, a label just displays a message so you don’t need to catch its events. Other controls such as buttons and scroll bars, however, are not very useful if the program cannot respond to their events.

You can take two approaches to catching a new control’s events. First, you can use the WithEvents keyword when you declare the control’s variable. Then you can open the form in the code editor, select the variable’s name from the left drop-down list, and select an event from the right drop-down list to give the control an event handler.

The following code demonstrates this approach. It declares a class-level variable btnHi using the WithEvents keyword. When you click the btnMakeHiButton button, its event handler initializes the variable to create the Hi button. It sets the control’s position and text, and adds it to the form’s Controls collection. When the user clicks this button, the btnHi_Click event handler executes and displays a message.

' Declare the btnHi button WithEvents.
Private WithEvents btnHi As Button
 
' Make the new btnHi button.
Private Sub btnMakeHiButton_Click() Handles btnMakeHiButton.Click
    btnHi = New Button()
    btnHi.SetBounds(16, 16, 80, 23)
    btnHi.Text = "Say Hi"
    Me.Controls.Add(btnHi)
End Sub
 
' The user clicked the btnHi button.
Private Sub btnHi_Click() Handles btnHi.Click
    MessageBox.Show("Hi")
End Sub

This first approach works if you know the number and types of the controls you will need ahead of time. Then you can define variables for them all using the WithEvents keyword. If you don’t know how many controls you need to create, however, this isn’t practical. For example, suppose that you want to create a button for each file in a directory. When the user clicks a button, the file should open. If you don’t know how many files the directory will hold, you don’t know how many variables you’ll need.

One solution to this dilemma is to use the AddHandler statement to add event handlers to the new controls. The following code demonstrates this approach. When you click the btnMakeHelloButton button, its Click event handler creates a new Button object, storing it in a locally declared variable. It sets the button’s position and text and adds it to the form’s Controls collection as before. Next, the program uses the AddHandler statement to make subroutine Hello_Click an event handler for the button’s Click event. When the user clicks the new button, subroutine Hello_Click displays a message.

' Make a new Hello button.
Private Sub btnMakeHelloButton_Click() Handles btnMakeHelloButton.Click
    ' Make the button.
    Dim btnHello As New Button()
    btnHello.SetBounds(240, 64, 80, 23)
    btnHello.Text = "Say Hello"
    Me.Controls.Add(btnHello)
 
    ' Add a Click event handler to the button.
    AddHandler btnHello.Click, AddressOf Hello_Click
End Sub
             
' The user clicked the Hello button.
Private Sub Hello_Click()
    MessageBox.Show("Hello")
End Sub

TAG, YOU’RE IT
When you build controls at run time, particularly if you don’t know how many controls you may create, the Tag property can be very useful. You can place something in a new control’s Tag property to help identify it. For example, you might store a control number in each new control’s Tag property and make them all use the same event handlers. The event handlers can check the Tag property to see which control raised the event.

You can use the same routine as an event handler for more than one button. In that case, the code can convert the sender parameter into a Button object and use the button’s Name, Text, and other properties to determine which button was pressed.

To remove a control from the form, simply remove it from the form’s Controls collection. To free the resources associated with the control, set any variables that refer to it to Nothing. For example, the following code removes the btnHi control created by the first example:

Me.Controls.Remove(btnHi)
btnHi = Nothing

This code can remove controls that you created interactively at design time, as well as controls you create during run time.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.166.149