Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
-
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
-
Error Handling (2)
-
VB6 Database


You are here: Visual Basic > VB6 (Beginners Tutorial)

Previous Page | Table of Contents | Next Page

Using a CheckBox control in Visual Basic 6

The CheckBox control is similar to the option button, except that a list of choices can be made using check boxes where you cannot choose more than one selection using an OptionButton. By ticking the CheckBox the value is set to True. This control can also be grayed when the state of the CheckBox is unavailable, but you must manage that state through code.

When you place a CheckBox control on a form, all you have to do, usually, is set its Caption property to a descriptive string. You might sometimes want to move the little check box to the right of its caption, which you do by setting the Alignment property to 1-Right Justify, but in most cases the default setting is OK. If you want to display the control in a checked state, you set its Value property to 1-Checked right in the Properties window, and you set a grayed state with 2-Grayed.

The only important event for CheckBox controls is the Click event, which fires when either the user or the code changes the state of the control. In many cases, you don't need to write code to handle this event. Instead, you just query the control's Value property when your code needs to process user choices. You usually write code in a CheckBox control's Click event when it affects the state of other controls. For example, if the user clears a check box, you might need to disable one or more controls on the form and reenable them when the user clicks on the check box again. This is how you usually do it (here I grouped all the relevant controls in one frame named Frame1):

Private Sub Check1_Click()
Frame1.Enabled = (Check1.Value = vbChecked)
End Sub

Note that Value is the default property for CheckBox controls, so you can omit it in code. I suggest that you not do that, however, because it would reduce the readability of your code.

The following example illustrates the use of CheckBox control

* Open a new Project and save the Form as CheckBox.frm and save the Project as CheckBox.vbp

* Design the Form as shown below

Object
Property
Setting
Form

Caption

Name

CheckBox

frmCheckBox

CheckBox

Caption

Name

Bold

chkBold

CheckBox

Caption

Name

Italic

chkItalic

CheckBox

Caption

Name

Underline

chkUnderline

OptionButton

Caption

Name

Red

optRed

OptionButton

Caption

Name

Blue

optBlue

OptionButton

Caption

Name

Green

optGreen

TextBox

Name

Text

txtDisplay

(empty)

CommandButton

Caption

Name

Exit

cmdExit

Following code is typed in the Click() events of the CheckBoxes

Private Sub chkBold_Click()
If chkBold.Value = 1 Then
  txtDisplay.FontBold = True
Else
  txtDisplay.FontBold = False
End If
End Sub

Private Sub chkItalic_Click()
If chkItalic.Value = 1 Then
  txtDisplay.FontItalic = True
Else
  txtDisplay.FontItalic = False
End If
End Sub

Private Sub chkUnderline_Click()
If chkUnderline.Value = 1 Then
  txtDisplay.FontUnderline = True
Else
  txtDisplay.FontUnderline = False
End If
End Sub

Following code is typed in the Click() events of the OptionButtons

Private Sub optBlue_Click()
  txtDisplay.ForeColor = vbBlue
End Sub

Private Sub optRed_Click()
  txtDisplay.ForeColor = vbRed
End Sub

Private Sub optGreen_Click()
  txtDisplay.ForeColor = vbGreen
End Sub

To terminate the program following code is typed in the Click() event of the Exit button

Private Sub cmdExit_Click()
  End
End Sub

Run the program by pressing F5. Check the program by clicking on OptionButtons and CheckBoxes.

See Also


  

Previous Page | Table of Contents | Next Page

   

Home | Link to Us | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved