Page 61 - Open Soource Technologies 304.indd
P. 61
Unit 3: Understanding Controls and Control Events
BackgroundImage: Used to set a background image for the CheckBox.
CheckAlign: Used to set the alignment for the CheckBox from a predefined list.
Checked: Default value is False, set it to True if you want the CheckBox to be displayed as
checked.
CheckState: Default value is Unchecked. Set it to True if you want a check to appear. When set
to Indeterminate it displays a check in gray background.
FlatStyle: Default value is Standard. Select the value from a predefined list to set the style of the
checkbox.
Important property in the Behavior section of the properties window is the Three State property
which is set to False by default. Set it to True to specify if the Checkbox can allow three check
states than two.
3.4.5.2 CheckBox Event
The default event of the CheckBox is the CheckedChange event which looks like this in code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
End Sub
3.4.5.3 Working with CheckBoxes
Lets work with an example. Drag a CheckBox (CheckBox1), TextBox (TextBox1) and a Button
(Button1) from the Toolbox.
Code to display some text when the Checkbox is checked
Private Sub CheckBox1_CheckedChanged(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Text = “CheckBox Checked”
End Sub
Code to check a CheckBox’s state
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
TextBox1.Text = "Checked"
Else
TextBox1.Text = "UnChecked"
End If
End Sub
Creating a CheckBox in Code
LOVELY PROFESSIONAL UNIVERSITY 55