JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Adding and Removing Controls at Run Time

You can add or remove controls to your application at run time-all you need to do is to use the form's Controls collection 's Add or Remove methods. We'll see collections later in the book; they let you operate on a number of objects at once, as with the Add and Remove methods.

Here's an example (AddControls on the CD-ROM) that adds a new text box at run time-all you have to do is to create a new text box in code, give it a size and location, set its text, and use the Add method when the user clicks a button:

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    Dim NewTextBox As New TextBox()
    NewTextBox.Size = New Size(100, 20)
    NewTextBox.Location = New Point(100, 100)
    NewTextBox.Text = "Hello from Visual Basic"
End Sub

You can see the results in Figure 4.22; when the user clicks a button, a new text box appears in the form.

Figure 4.22: Adding controls at run time.

Related solutions:

Found on page:

Creating a LinkLabel in Code


Adding Controls to Group Boxes in Code


Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor