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
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.
In Solution Explorer, select Form1.vb, and then on the View menu choose Designer.
From the Toolbox, drag a GroupBox control onto the form.
In the Properties window, change the Text properties of RadioButton3 and RadioButton4 to Thin crust and Thick crust, respectively.
In the form, double-click the Order Pizza button to open the Button1_Click event handler in the Code Editor.
In the Button1_Click event handler, add the following code.
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.