Lesson VII.            Frames and OptionButtons

 

Objectives

ü       To use the Frame and OptionButton controls

ü       To show how Frames work with OptionButtons

 

 

 


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 don’t 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). 

 

 

 

 

 

 

 


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.

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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

Color

vbBlack 

Black

VbGreen

Green

VbYellow

Yellow

VbMagenta

Magenta

VbCyan

Cyan

VbWHite

White

 

Save your work as Lesson7.vbp.

 

 


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.