OPTION BUTTONS

 

Option buttons, also called radio buttons, are typically used in a group of two or more.  At any one time, only one button in the group can be "on".  Clicking an option button turns it "on" and turns all other buttons in the group "off".

 

Option button groups operate in a container control, such as a frame.  Therefore, different sets of option button groups should be placed in their own frame on the form.  Recall how to place a control directly inside a frame: either (1) single click it from the toolbox and draw it in, or  (2) double-click it from the toolbox, cut it from the form, and paste it into the frame.  If a group of option buttons is not contained within a frame, then the form itself acts as their container.

 

In code, to perform an action based on which option button the user clicked, do one of two things:

(1)        To perform an action as soon as the user clicks an option button, place code in the Click event of the option button.

(2)                 To perform a "delayed" action, such as when the user clicks a command button, check the Value property of the option buttons in the group.  If the Value = True, then the button is "on".

 

Examples using both methods follow. (Note: The examples in this topic use the built-in color constants vbRed, vbGreen, and vbBlue. More information on color constants is provided in the section at the end of this topic.)

 

First, a frame containing three option buttons, named optRed, optGreen, and optBlue respectively was placed on the form shown below:

 

 

Next, code was written for the Click event of each of the option buttons. The code causes the background color of the form to change to the appropriate color when one of the option buttons is clicked:

 

Private Sub optRed_Click()

    Form1.BackColor = vbRed

End Sub

 

Private Sub optGreen_Click()

    Form1.BackColor = vbGreen

End Sub

 

Private Sub optBlue_Click()

    Form1.BackColor = vbBlue

End Sub

 

If the program was to be run at this point, you would get immediate results when you clicked one of the option buttons – as soon as you clicked one of the option buttons, the form would change color.

 

To demonstrate the "delayed" action, let's say the above program was modified as follows: First,  a command button named cmdChangeColor is placed on the form:

 

 

Second, all code from the option buttons' Click event (shown in the previous example above) is removed.

 

Third, code for the cmdChangeColor_Click event is written as follows:

 

Private Sub cmdChangeColor_Click()

 

    If optRed.Value = True Then

        Form1.BackColor = vbRed

    ElseIf optGreen.Value = True Then

        Form1.BackColor = vbGreen

    Else

        Form1.BackColor = vbBlue

    End If

   

End Sub

 

If the program was to be run at this point, clicking on one of the option buttons would not cause an immediate result – it would simply set the Value of the clicked button to True. Only when you clicked the "Change Color" button would the background color of the form change, based on the code above.

 

Syntax Notes:

 

The Value property is the default property of the option button and can therefore be dropped when coding, as in:

 

            If optRed = True Then . . .

 

Since Value is a Boolean property, you can drop the "= True" from the conditional expression, making it possible to cut the coding down further to:

 

            If optRed Then . . .

 

Coding an Option Button Control Array

 

Using the same scenarios as in the examples above, suppose we used a control array for the set of three option buttons (a control array called optColor indexed 0 through 2 instead of the individual buttons optRed, optGreen, and optBlue).

 

The coding for the "immediate" action (using the option button's click event) would be:

 

Private Sub optColor_Click(Index As Integer)

 

    Select Case Index

        Case 0   

     Form1.BackColor = vbRed

        Case 1   

     Form1.BackColor = vbGreen

        Case 2                         ' could also use "Case Else" here     

     Form1.BackColor = vbBlue

                  End Select

 

End Sub

 

The coding for the "delayed" action (using a command button) would be:

 

Private Sub cmdChangeColor_Click()

 

    Dim intLoopCtr As Integer

    Dim intIndex   As Integer

 

    For intLoopCtr = 0 To 2

        If optColor(intLoopCtr).Value = True Then

            intIndex = intLoopCtr

            Exit For

        End If

    Next

   

    Select Case intIndex

        Case 0   

     Form1.BackColor = vbRed

        Case 1   

     Form1.BackColor = vbGreen

        Case 2                         ' could also use "Case Else" here     

     Form1.BackColor = vbBlue

                  End Select

   

End Sub

 

 

The sample program exercises each of the scenarios discussed above. A screen shot is shown below:

 

 

Download the VB project code for the example above here.

 

Color Constants

 

VB provides a set of built-in constants that can be used to refer to colors. Some of these were used in the sample program presented in this topic. The available constants, from MSDN, are listed below:

 

Colors

 

Constant

Value

Description

vbBlack

&H0

Black

vbRed

&HFF

Red

vbGreen

&HFF00

Green

vbYellow

&HFFFF

Yellow

vbBlue

&HFF0000

Blue

vbMagenta

&HFF00FF

Magenta

vbCyan

&HFFFF00

Cyan

vbWhite

&HFFFFFF

White

System Colors

Constant

Value

Description

vbScrollBars

&H80000000

Scroll bar color

vbDesktop

&H80000001

Desktop color

vbActiveTitleBar

&H80000002

Color of the title bar for the active window

vbInactiveTitleBar

&H80000003

Color of the title bar for the inactive window

vbMenuBar

&H80000004

Menu background color

vbWindowBackground

&H80000005

Window background color

vbWindowFrame

&H80000006

Window frame color

vbMenuText

&H80000007

Color of text on menus

vbWindowText

&H80000008

Color of text in windows

vbTitleBarText

&H80000009

Color of text in caption, size box, and scroll arrow

vbActiveBorder

&H8000000A

Border color of active window

vbInactiveBorder

&H8000000B

Border color of inactive window

vbApplicationWorkspace

&H8000000C

Background color of multiple-document interface (MDI) applications

vbHighlight

&H8000000D

Background color of items selected in a control

vbHighlightText

&H8000000E

Text color of items selected in a control

vbButtonFace

&H8000000F

Color of shading on the face of command buttons

vbButtonShadow

&H80000010

Color of shading on the edge of command buttons

vbGrayText

&H80000011

Grayed (disabled) text

vbButtonText

&H80000012

Text color on push buttons

vbInactiveCaptionText

&H80000013

Color of text in an inactive caption

vb3DHighlight

&H80000014

Highlight color for 3D display elements

vb3DDKShadow

&H80000015

Darkest shadow color for 3D display elements

vb3DLight

&H80000016

Second lightest of the 3D colors after vb3Dhighlight

vb3DFace

&H8000000F

Color of text face

vb3Dshadow

&H80000010

Color of text shadow

vbInfoText

&H80000017

Color of text in ToolTips

vbInfoBackground

&H80000018

Background color of ToolTips