Getting Information from the User
Check Box
A Check Box displays several Yes-No, True-False choices to the user—from which one or more choices can be made.
When a selection is made, a check appears in the box associated with the choice. Note the section below the label area of the Check Box Example window. It's a Check Box control.
To check whether one or more of the items are checked via Visual Basic, you need only check the item's Value (see the middle column of the table below).
By default, the check box control is set to vbUnchecked.
The following table lists the values and VB constants used to set the Value Property:
|
Status of the Check Box
|
Value
|
VB Constant
|
|
Unchecked
|
0
|
vbUnchecked
|
|
Checked
|
1
|
vbChecked
|
|
Grayed Out (unavailable)
|
2
|
vbGrayed
|
|
Take a look at the code example below. Assume we have text in a label named lblFeedback. Then, if the user clicks on "Underline", we can underline the text in that panel by using the code below.
CODE EXAMPLE
Private Sub chkUnderline_Click()
'Check to see if Underline checkbox is checked
' If true, underline the text in the lblFeedback box
' If false, don't underline the text
If chkUnderline.Value = vbChecked Then
lblFeedback.FontUnderline = True
Else
lblFeedback.FontUnderline = False
End If
End Sub
Go Back to Top
Option Buttons (aka Radio Buttons)
The round buttons in the Single Radio Button Example below are Option Buttons. Option buttons are used to display several choices to the user—from which one and only one choice can be selected. If the user clicks on each choice, a black dot appears in the circular area associated with that choice, signifying that the "button" is "pushed in."
In the example window to the left, only one choice can be "pushed in" at any one time. If any other choice had been selected before Yellow was selected, it would be automatically de-selected.
So if you need for it to be possible for the user to select more than one choice simultaneously, you have to do something a little different—use a Frame Control (a type of "Container Control". Read on.
Grouping Options with a Frame Control
In the Double Frame Radio Button Example below, note the rectangles surrounding both the Foreground and Background option buttons. They're called "Frame Controls". They're used for separating the two groupings so that, in each group, the user can select one choice.
These Frame Controls have properties associated with them just as any other object in Visual Basic. Change the Caption property and you change the title of the Frame Control (e.g., Foreground and Background in the Double Frame Radio Button Example).
WARNING: Create the Frame Controls on the form first, then the radio buttons within each frame. If you create the radio buttons first and then the Frame Control, the Frame Control won't recognize the buttons as being in different groupings—all the radio buttons will be treated as if they were one big group (as in the Single Frame Radio Button Example in the previous section).
If you make the mistake of creating the buttons first and then the Frame Control, you can rectify the mistake without starting over by "cutting" the radio buttons out of the Frame Control and then "pasting" them back in. Then the Frame Control will recognize the radio buttons pasted in.
To determine whether an option button has been selected or to select an option button via code use the option button's Value property (e.g., optTextColor.Value = True)
Go Back to Top
Return to the Visual Basic Page
|