The CheckBox Control


In this lesson, I'll give you an overview of the CheckBox control in VB6. This is obviously a powerful tool in the Visual Basic 6 IDE.

Unlike the OptionButton control, the CheckBox control lets you perform multiple selections, meaning that the end user of your finished software product will be able to have multiple choices from a list of items. 



This control has three states : checked, unchecked and grayed. The value property determines the checkbox state.


Example:

Private Sub cmdShow_Click()
    If Check1.Value = 1 Then
        Print "Checked !"
    ElseIf Check1.Value = 0 Then
        Print "Unchecked !"
    End If
End Sub

Output:

One CheckBox control


If you assign Check1.Value =1 then the CheckBox is checked and if Check1.Value = 0 then it is unchecked. If Check1.Value = 2 then the checkbox is grayed.




Example: The previous program can also be written in the following way.

Private Sub cmdShow_Click()
    If Check1.Value = vbChecked Then
        Print "Checked !"
    ElseIf Check1.Value = vbUnchecked Then
        Print "Unchecked !"
    End If
End Sub

Output:

 One checked check box

'vbChecked' and 'vbUnchecked' are vb constants whose values are 1 and 0 respectively.

The grayed state can be set using the 'vbGrayed' vb constant whose value is 2.



Example: Program to make multiple choices.

Private Sub cmdShow_Click()
     If chkTea.Value = 1 Then
         Print "Tea"
     End If


     If chkCoffee.Value = 1 Then
         Print "Coffee"
     End If


     If chkPizza.Value = 1 Then
         Print "Pizza"
     End If


     If chkChocolate.Value = 1 Then
         Print "Chocolate"
     End If
End Sub

Output: 

An example with four checkbox controls



I've wrtiten a small program to demonstrate how to use the option button controls in your project.






Go to the following link for code and explanation of the above program sample:


Apart from checked and unchecked states, there is another state called grayed state. The grayed state is used to indicate that the checBox is unavailable. 

Grayed state:

example of a grayed checkbox
The grayed state can be set using the value property from the Properties Window or you may want to set this property in run-time.

Example:

Check1.Value = vbGrayed
Or,
Check1.Value = 2