|
CheckBox
CheckBoxes are those controls which gives us an option to select, say, Yes/No or True/False.
A checkbox is clicked to select and clicked again to deselect some option. When a
checkbox is selected a check (a tick mark) appears indicating a selection. The CheckBox
control is based on the TextBoxBase class which is based
on the Control class. Below is the image of a Checkbox.
Notable Properties
Important properties of the CheckBox in the Appearance section of the properties window
are:
Appearance: Default value is Normal. Set the value to Button
if you want the CheckBox to be displayed as a Button.
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 ThreeState property
which is set to False by default. Set it to True to specify if the Checkbox can allow
three check states than two.
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
|
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
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e
As_
System.EventArgs) Handles_ MyBase.Load
Dim CheckBox1 As New CheckBox()
CheckBox1.Text = "Checkbox1"
CheckBox1.Location = New Point(100, 50)
CheckBox1.Size = New Size(95, 45)
Me.Controls.Add(CheckBox1)
End Sub
|
|