Selecting Individual Options with Check Boxes

A check box indicates whether a particular condition is on or off. You use check boxes in an application to give users true/false or yes/no options. Because check boxes work independently of each other, a user can select any number of check boxes at the same time. For example in Figure 3.9, Bold and Italic can both be checked.

Figure 3.9   Check boxes

The Check Box Application

The Check Box example uses a check box to determine whether the text is displayed in regular or italic font. For a working version of this example, see Check.frm in the Controls.vbp sample application.

The application has a text box, a label, a command button, and two check boxes, as shown in Figure 3.10.

Figure 3.10   Check box example

The following table lists the property settings for the objects in the application.

Object Property Setting
Form Name
Caption
frmCheck
Check Box Example
Text box Name
Text
txtDisplay
Some sample text
First Check box Name
Caption
chkBold
&Bold
Second Check box Name
Caption
chkItalic
&Italic
Command button Name
Caption
cmdClose
&Close

When you check Bold or Italic, the check box's Value property is set to 1; when unchecked, its Value property is set to 0. The default Value is 0, so unless you change Value, the check box will be unchecked when it is first displayed. You can use the constants vbChecked and vbUnchecked to represent the values 1 and 0.

Events in the Check Box Application

The Click event for the check box occurs as soon as you click the box. This event procedure tests to see whether the check box has been selected (that is, if its Value = vbChecked). If so, the text is converted to bold or italic by setting the Bold or Italic properties of the Font object returned by the Font property of the text box.

Private Sub chkBold_Click ()
   If ChkBold.Value = vbChecked Then   ' If checked.
      txtDisplay.Font.Bold = True
   Else                           ' If not checked.
      txtDisplay.Font.Bold = False
   End If
End Sub

Private Sub chkItalic_Click ()
   If ChkItalic.Value = vbChecked Then   ' If checked.
      txtDisplay.Font.Italic = True
   Else                           ' If not checked.
      txtDisplay.Font.Italic = False
   End If
End Sub