JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Adding Controls to Group Boxes in Code

Here's an example, AddControlsGroupBox on the CD-ROM, where I'm creating a group box at run time, placing three radio buttons into the group box, and adding an event handler for each radio button. Note that as when we added controls to a panel (see "Adding Controls to Panels in Code" earlier in this chapter), I use the group box's Controls collection's Add method to add controls to a group box. To connect the new radio buttons to event handlers, I use the AddHandler function:

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim GroupBox1 As New GroupBox()
    Dim WithEvents RadioButton1 As RadioButton
    Dim WithEvents RadioButton2 As RadioButton
    Dim WithEvents RadioButton3 As RadioButton

    'Windows Form Designer generated code

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        RadioButton1 = New RadioButton()
        RadioButton2 = New RadioButton()
        RadioButton3 = New RadioButton()

        GroupBox1.Text = "GroupBox1"
        GroupBox1.Location = New Point(40, 40)
        GroupBox1.Size = New Size(200, 100)

        RadioButton1.Location = New Point(16, 16)
        RadioButton1.Text = "RadioButton1"
        RadioButton1.Size = New Size(120, 16)

        RadioButton2.Location = New Point(16, 32)
        RadioButton2.Text = "RadioButton2"
        RadioButton2.Size = New Size(120, 20)

        RadioButton3.Location = New Point(16, 48)
        RadioButton3.Text = "RadioButton3"
        RadioButton3.Size = New Size(120, 20)



        AddHandler RadioButton1.CheckedChanged, AddressOf _
        AddHandler RadioButton2.CheckedChanged, AddressOf _
        AddHandler RadioButton3.CheckedChanged, AddressOf _
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As _
        System.Object, ByVal e As System.EventArgs) Handles _
        TextBox1.Text = "You clicked radio button 1"
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As _
        System.Object, ByVal e As System.EventArgs) Handles _
        TextBox1.Text = "You clicked radio button 2"
    End Sub

    Private Sub RadioButton3_CheckedChanged(ByVal sender As _
        System.Object, ByVal e As System.EventArgs) Handles _
        TextBox1.Text = "You clicked radio button 3"
    End Sub
End Class

You can see this example at work in Figure 6.18. When the user clicks the button in this example, it creates a new group box, adds three radio buttons to the group box, and adds an event handler to each radio button. As you can see in Figure 6.18, the user can now click radio buttons and the program indicates which radio button was clicked in the text box.

Figure 6.18: Creating a group box at run time.

Related solution:

Found on page:

Adding and Removing Controls at Run Time


Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor