Export (0) Print
Expand All

Closer Look: Using Multiple Groups of Radio Buttons

In this lesson, you will learn how to create multiple groups of mutually exclusive radio buttons on a single form.

In the previous lesson, you learned how to create a group of radio buttons to present a set of mutually exclusive choices to the user. What happens, though, if you present two or more sets of choices? You will find that all RadioButton controls on a form are treated as a single group, allowing only one radio button to be selected.

Fortunately, Visual Basic has several controls known aptly as container controls that can contain other controls. By placing a container control on the form and then placing RadioButton controls inside the container control, you can have multiple groups of radio buttons on the same form.

The most common container controls are the GroupBox control and the Panel control. The main difference between the two is that the GroupBox control has a visible border around it, and the Panel does not. When using a container control to group radio buttons, the GroupBox control is the best choice, because the border provides a visual cue that the group of choices belongs together.

To use a GroupBox as a container

  1. Open the UserChoices project that you created in the previous lesson. If you didn't save it, you will need to first go back to the previous lesson, Getting User Choices: Using Check Boxes and Radio Buttons, and complete the procedures.

  2. In Solution Explorer, select Form1.vb, and then on the View menu choose Designer.

  3. From the Toolbox, drag a GroupBox control onto the form.

  4. In the Properties window, change the Text property for the GroupBox control to read Select a crust.

  5. With the GroupBox control still selected, drag two RadioButton controls from the Toolbox, and drop them on top of the GroupBox control.

  6. In the Properties window, change the Text properties of RadioButton3 and RadioButton4 to Thin crust and Thick crust, respectively.

  7. In the form, double-click the Order Pizza button to open the Button1_Click event handler in the Code Editor.

  8. In the Button1_Click event handler, add the following code.

    If RadioButton3.Checked = True Then
        MsgBox("You chose a thin crust")
        MsgBox("You chose a thick crust")
    End If
  9. Press F5 to run your program. Choose one of the radio buttons, and then click the Order Pizza button. A message box noting your choice is displayed. Notice that your choice of sauce is preserved.

In this lesson, you learned how to use a container control to group RadioButton controls. In the next lesson, you will learn how to display pictures.

Next Lesson: Picture This: Displaying Images

Community Additions

© 2014 Microsoft