You are on page 1of 2

Lesson VII.

Frames and OptionButtons

Objectives

To use the Frame and OptionButton controls


To show how Frames work with OptionButtons

Notes
OptionButtons give users an array of choices. By clicking an OptionButton, the user selects or
deselects an OptionButton. It has a black dot inside when it is selected.
OptionButtons are sometimes called radio buttons. The user can only select one button among a
set of buttons. Buttons are grouped in sets with the use of another Visual Basic control called
Frame. A Frame is a rectangular region that holds other controls and groups these into a single set.
When one button is selected in a set of option buttons, the rest are automatically deselected by
Visual Basic. Thus, OptionButtons are useful in cases like when you want the user to select a
gender (either male or female; one can never be both male and female).
OptionButtons have similar properties with that of a Label. The text that you see beside the
OptionButton is its caption property; hence, you dont have to place a Label to identify it.
Warning: When option buttons are not placed in Frames, Visual Basic assumes that the option
buttons are independent of each other. Thus, the user can select any of them at the same
time (in our example above, the user can be both male and female).

Option Button

Frame

How do you know if an OptionButton is selected? Each OptionButton has a Value property that
contains either True or False. When the value of this property is True, the button is currently
selected. Otherwise, it is deselected.

Lesson in Action

optBlue
optRed

Create the above Form and write the following procedures:


Private Sub optBlue_Click()
lblColor.Caption = "BLUE"
lblColor.BackColor = vbBlue
End Sub
Private Sub optRed_Click()
lblColor.Caption = "RED"
lblColor.BackColor = vbRed
End Sub
What the above procedures do is to format the Label based on which OptionButton is selected. If
the user selects Blue, VB changes the background color of the Label to blue (through the BackColor
property) and sets its caption property to Blue.

vbRed and vbBlue are color named literals. Since hex color formats are difficult to remember, VB
provides you with these values that represent hex values of commonly used colors. The other
colors aside from vbBlue and vbRed are as follows:
Literal
vbBlack
VbGreen
VbYellow
VbMagenta
VbCyan
VbWHite

Color
Black
Green
Yellow
Magent
a
Cyan
White

Save your work as Lesson7.vbp.

On your Own
Instructions: Create a short math drill. The drill contains 3 items. When the user presses a
button labeled Check Score, a Message Box appears revealing the his score (e.g. You correctly
answered 2 items!). The following is how the Form should look like.

You might also like