JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Radio Button Lists

At design time, you can add items to a radio button list by clicking the Items property in the Properties window, opening the ListItem Collection Editor you see in Figure 16.7, where I'm using it on the radio button list in the RadioButtons example on the CD-ROM which appears in Figure 16.2. (You can see the code for this example in "Creating Radio Buttons" in this chapter.) This collection editor works much like other collection editors—to add a new item to the radio button list control, click the Add button, and fill in its Text (the checkbox's caption), Value (holds optional text associated with the checkbox), and Selected (set this to True to make the corresponding checkbox appear selected initially) properties. You also can add radio buttons to a radio button list at run time, using the Add method of the control's Items collection.

Click To expand
Figure 16.7: The ListItem Collection Editor.

How can you handle selection events in a radio button list? You can use the SelectedIndexChanged event, and use the radio button list's SelectedIndex and SelectedItem properties to get the selected index and item respectively.

You can see this at work in the RadioButtons example. In this case, to display the selected color of the car or truck the user has selected, I just use the SelectedItem property to get the selected item in the radio button list (only one item can be selected at a time, of course), and use that item's Value property to get the selected color:

    Private Sub RadioButtonList1_SelectedIndexChanged(ByVal sender _
        As System.Object, ByVal e As System.EventArgs) Handles _
        RadioButtonList1.SelectedIndexChanged
        If RadioButton1.Checked Then
            TextBox2.Text = "Your car is: "
        Else
            TextBox2.Text = "Your truck is: "
        End If

        TextBox2.Text &= RadioButtonList1.SelectedItem.Value
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As _
        System.Object, ByVal e As System.EventArgs) Handles _
        RadioButton1.CheckedChanged
        If CType(sender, RadioButton).Checked Then
            TextBox1.Text = "Car is selected."
        End If

        If RadioButton1.Checked Then
            TextBox2.Text = "Your car is: "
        Else
            TextBox2.Text = "Your truck is: "
        End If

        TextBox2.Text &= RadioButtonList1.SelectedItem.Value
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As _
        System.Object, ByVal e As System.EventArgs) Handles _
        RadioButton2.CheckedChanged
        If CType(sender, RadioButton).Checked Then
            TextBox1.Text = "Truck is selected."
        End If

        If RadioButton1.Checked Then
            TextBox2.Text = "Your car is: "
        Else
            TextBox2.Text = "Your truck is: "
        End If

        TextBox2.Text &= RadioButtonList1.SelectedItem.Value
    End Sub

And that's all it takes—you can see the results of this code in Figure 16.2.

Note 

Don't forget—if you want to handle this control's events immediately, you must set its AutoPostBack property to True.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor