Getting User Choices: Using Check Boxes and Radio Buttons

In this lesson, you will learn how to use check boxes and radio buttons to present and retrieve user choices.

When you create the user interface for your program, you often need a way to present choices. For example, suppose you wrote an application to take orders for a pizza restaurant—you might want to allow users to select any of a variety of toppings. The CheckBox control provides a visual representation that makes this option easy to create.

The CheckBox control consists of a text label and a box that the user can select. When the user clicks the box, a check mark appears in the box. If the box is clicked again, the check mark is removed. The status of the check box can be retrieved by using the CheckBox.Checked property. If the box displays a check mark, the property returns True. If no check is displayed, the property returns False.

To use check boxes

  1. On the File menu, click New Project.

  2. In the New Project dialog box, in the Templates pane, click Windows Application.

  3. In the Name box, type UserChoices and then click OK.

    A new Windows Forms project opens.

  4. From the Toolbox, drag a Button and three CheckBox controls onto the form.

  5. In the Properties Window, change the Text property of CheckBox1, CheckBox2, and CheckBox3 to read Cheese, Peppers, and Mushrooms, respectively.

  6. In the Properties window, change the Text property of Button1 to read Order Pizza.

  7. In the form, double-click the button; this opens the Button1_Click event handler in the Code Editor.

  8. Add the following code to the Button1_Click event handler:

    Dim toppings As String = ""
    If CheckBox1.Checked = True Then
        toppings &= "Cheese "
    End If
    If CheckBox2.Checked = True Then
        toppings &= "Peppers "
    End If
    If CheckBox3.Checked = True Then
        toppings &= "Mushrooms"
    End If
    If toppings <> "" Then
        MsgBox("Your pizza has the following toppings: " & toppings)
    End If
  9. Press F5 to run your program. When the form appears, select some toppings and click the button. A message box indicating your choice of pizza toppings is displayed.

You just learned how to enable a user to choose any of several options. But what if you want the user to choose only one of several options? In this case, you can use the RadioButton control.

Unlike check boxes, radio buttons (also known as option buttons) always work as part of a group. Selecting one radio button immediately clears all the other radio buttons in the group. Defining a group of radio buttons tells the user, "Here is a set of choices from which you can choose one and only one."

You can use groups of RadioButton controls to enable users to choose between exclusive options. For example, you might allow users to choose either regular sauce or spicy sauce on their pizza, but not both. Like a CheckBox control, you can get information about the status of the RadioButton control from the RadioButton.Checked property.

To use radio buttons

  1. From the Toolbox, drag two RadioButton controls onto the form.

  2. In the Properties window, set the Text property for RadioButton1 to Regular Sauce.

  3. Set the Checked property for RadioButton1 to True.


    When defining a group of choices, you should always set one choice to be the default.

  4. In the Properties window, set the Text property for RadioButton2 to Spicy Sauce.

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

  6. In the Button1_Click event handler, add the following code:

    If RadioButton1.Checked = True Then
        MsgBox("You chose regular sauce")
        MsgBox("You chose spicy sauce")
    End If
  7. 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.

    Try to select both radio buttons at the same time. Notice that the radio buttons are exclusive. After you click one, the other is cleared automatically.

In this lesson, you learned how to use the CheckBox and RadioButton controls to give users choices in the user interface. At this point, you can either go to the next lesson, Displaying Images: Using the PictureBox Control, or learn how to create multiple groups of radio buttons with Closer Look: Using Multiple Groups of Radio Buttons. If you choose the second option, you should save your UserChoices project for use in the next lesson.