CheckBox Control in VB.NET
In this article you will learn how to u seCheckBox Control (Windows Forms) in VB.NET.
CheckBox Control (Windows Forms)
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.
The CheckBox
control has two important properties, Checked and
CheckState. The Checked property returns either
true or false. The CheckState
property returns either CheckState.Checked or
CheckState.Unchecked; or, if the ThreeState property
is set to true, CheckState may also return
CheckState.Indeterminate. In the indeterminate state, the box
is displayed with a dimmed appearance to indicate the option is unavailable.
Other Properties :
-
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.
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
Windows Forms CheckBox Clicks
Whenever a user clicks a Windows Forms
CheckBox control, the Click event occurs. You can
program your application to perform some action depending upon the state of
the check box.
To
respond to CheckBox clicks
-
In the Click event
handler, use the Checked property to determine the
control's state, and perform any necessary action
Private Sub
CheckBox1_Click(ByVal sender
As Object,
ByVal e As
System.EventArgs) Handles
CheckBox1.Click
' The CheckBox control's Text
property is changed each time the
' control is clicked,
indicating a checked or unchecked state.
If CheckBox1.Checked =
True Then
CheckBox1.Text =
"Checked"
Else
CheckBox1.Text =
"Unchecked"
End
If
End Sub
Output :

Setting Options with Windows Forms CheckBox Controls
A Windows Forms
CheckBox control is used to give users True/False or Yes/No options. The
control displays a check mark when it is selected.
To set
options with CheckBox controls
-
Examine the value of the Checked
property to determine its state, and use that value to set an option.
In the code sample below, when
the CheckBox control's CheckedChanged
event is raised, the form's AllowDrop property is set to
false if the check box is checked. This is useful for
situations where you want to restrict user interaction.
Private Sub
CheckBox1_CheckedChanged(ByVal sender
As System.Object, _
ByVal e As
System.EventArgs) Handles
CheckBox1.CheckedChanged
' Determine the CheckState of the check
box.
If CheckBox1.CheckState =
CheckState.Checked Then
' If checked, do not allow
items to be dragged onto the form.
Me.AllowDrop =
False
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