In this lesson, I’ll give you an overview of the CheckBox control in VB6. This is 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
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
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
Apart from checked and unchecked states, there is another state called grayed state. The grayed state is used to indicate that the checkbox is unavailable.
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
The attached project, The_Checkbox_Control, gives you a good idea of using the checkbox control.