Visual Studio 2010 CheckBox control in VB.NET

In this article, we will learn how to use a CheckBox control in a windows forms application using Visual Studio 2010.
  • 19511

In this article, we will learn how to use a CheckBox control in a Windows Forms application using Visual Studio 2010.

CheckBox control

CheckBox control enables the user to select and deselect the associated option. When a checkbox is selected a check (a tick mark) appears indicating a selection. Below is the image of CheckBox:

check-box4.gif
 

Figure 1.

CheckBox properties:

These are some important properties of the CheckBox control:

Appearance -  Property used to set or get the appearance of a checkbox.

AutoCheck -  Property used to specify whether to change appearance automatically when the checkbox is clicked.

Checked - Property is used to set or get the value indicating if the checkbox is in checked state.

CheckState - Property is used to set or get state of a three state checkbox.

FlatStyle - Property set or get flatstyle appearance of the checkbox.

Image - Property is used to get or set the image displayed in checkbox.

ImageList - Property used to set or get the image list that contains the image displayed in a checkbox.

ThreeState - Property is used to specify if the checkbox will allow three check states rather than two.

check-box1.gif
 

Figure 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

Creating 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)

        ' check box will be create.

    End Sub

In the above code controls is a class.

Working with CheckBox:

Drag a CheckBox ,TextBox and a Button from the Toolbox. Form looks like this.

check-box2.gif
 

Figure 3.

Now double click on the button and write this code.

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

Now run the application and select checkbox and click on button.

check-box3.gif
 

Figure 4.

Conclusion:

Hope the article would have helped you in understanding CheckBox control in VB.NET.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.