ARTICLE

CheckedListBox in VB.NET

Posted by Mahesh Chand Articles | Windows Forms VB.NET June 03, 2010
A CheckedListBox control is a ListBox control with CheckBox displayed in the left side where user can select a single or multiple items. In this article, I will discuss how to create a CheckedListBox control in Windows Forms at design-time as well as run-time. After that, I will continue discussing various properties and methods available for the CheckedListBox control.
 
Reader Level:

CheckedListBox Control
A CheckedListBox control is a ListBox control with CheckBox displayed in the left side where user can select a single or multiple items. In this article, I will discuss how to create a CheckedListBox control in Windows Forms at design-time as well as run-time. After that, I will continue discussing various properties and methods available for the CheckedListBox control.

Creating a CheckedListBox

We can create a CheckedListBox control using a Forms designer at design-time or using the CheckedListBox class in code at run-time (also known as dynamically).

To create a CheckedListBox control at design-time, you simply drag and drop a CheckedListBox control from Toolbox to a Form in Visual Studio. After you drag and drop a CheckedListBox on a Form, the CheckedListBox looks like Figure 1. Once a CheckedListBox is on the Form, you can move it around and resize it using mouse and set its properties and events.

CheckedListBoxImg1.jpg
Figure 1

Creating a CheckedListBox control at run-time is merely a work of creating an instance of CheckedListBox class, set its properties and adds CheckedListBox class to the Form controls.

First step to create a dynamic CheckedListBox is to create an instance of CheckedListBox class. The following code snippet creates a CheckedListBox control object.

CheckedListBox1 = New CheckedListBox()

 

In the next step, you may set properties of a CheckedListBox control. The following code snippet sets location, width, height, background color, foreground color, Text, Name, and Font properties of a CheckedListBox.

CheckedListBox1.Location = New System.Drawing.Point(12, 12)

CheckedListBox1.Name = "CheckedListBox1"

CheckedListBox1.Size = New System.Drawing.Size(245, 169)

CheckedListBox1.BackColor = System.Drawing.Color.Orange

CheckedListBox1.ForeColor = System.Drawing.Color.Black

CheckedListBox1.FormattingEnabled = True

 

Once a CheckedListBox control is ready with its properties, next step is to add the CheckedListBox control to the Form. To do so, we use Form.Controls.Add method. The following code snippet adds a CheckedListBox control to the current Form.

 

Controls.Add(CheckedListBox1)

 

Setting CheckedListBox Properties

After you place a CheckedListBox control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 2.

CheckedListBoxImg2.jpg
Figure 2

Location, Height, Width, and Size

The Location property takes a Point that specifies the starting position of the CheckedListBox on a Form. You may also use Left and Top properties to specify the location of a control from the left top corner of the Form.  The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The following code snippet sets Location, Width, and Height properties of a CheckedListBox control.

CheckedListBox1.Location = New System.Drawing.Point(12, 12)

CheckedListBox1.Size = New System.Drawing.Size(245, 169)

CheckedListBox1.Width = 300

CheckedListBox1.Height = 400

Background, Foreground, BorderStyle

BackColor and ForeColor properties are used to set background and foreground color of a CheckedListBox respectively. If you click on these properties in Properties window, the Color Dialog pops up.

Alternatively, you can set background and foreground colors at run-time. The following code snippet sets BackColor, ForeColor, and BorderStyle properties.

CheckedListBox1.BackColor = System.Drawing.Color.Orange

CheckedListBox1.ForeColor = System.Drawing.Color.Black

CheckedListBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle

 

The new CheckedListBox with background, foreground and border style looks like Figure 3.

 

CheckedListBoxImg3.jpg
Figure 3

Name

Name property represents a unique name of a CheckedListBox control. It is used to access the control in the code. The following code snippet sets and gets the name and text of a CheckedListBox control.

CheckedListBox1.Name = "CheckedListBox1"

Font

Font property represents the font of text of a CheckedListBox control. If you click on the Font property in Properties window, you will see Font name, size and other font options. The following code snippet sets Font property at run-ti

CheckedListBox1.Font = new Font("Georgia", 16)

CheckedListBox Items

The Items property is used to add and work with items in a CheckedListBox. We can add items to a CheckedListBox at design-time from Properties Window by clicking on Items Collection as you can see in Figure 4.

CheckedListBoxImg4.jpg
Figure 4

When you click on the Collections, the String Collection Editor window will pop up where you can type strings. Each line added to this collection will become a CheckedListBox item. I add four items as you can see from Figure 5.

 

CheckedListBoxImg5.jpg
Figure 5

The CheckedListBox looks like Figure 6.

CheckedListBoxImg6.jpg
Figure 6

You can add same items at run-time by using the following code snippet.

CheckedListBox1.Items.AddRange(New Object() _
{"Send Newsletter", "Send me Tip of of the Day", "Send me Deals", "Authentication"})

 

We can also add these items one by one at run-time by using the following code snippet.

CheckedListBox1.Items.Add("Send Newsletter")

CheckedListBox1.Items.Add("Send me Tip of of the Day")

CheckedListBox1.Items.Add("Send me Deals")

CheckedListBox1.Items.Add("Authentication")

Getting All Items

To get all items, we use the Items property and loop through it to read all the items.  The following code snippet loops through all items and adds item contents to a StringBuilder and displays in a MessageBox.

Dim sb As New System.Text.StringBuilder

        For Each item In CheckedListBox1.Items

            sb.Append(item)

            sb.Append(" ")

        Next

MessageBox.Show(sb.ToString())

Getting Selected Items

The SelectedItems property returns all selected items in a CheckedBoxList.  The following code snippet loops through all selected items and adds item contents to a StringBuilder and displays in a MessageBox.

Dim sb As New System.Text.StringBuilder

        For Each item In CheckedListBox1.SelectedItems

            sb.Append(item)

            sb.Append(" ")

        Next

        MessageBox.Show(sb.ToString())

Getting Checked Items

The CheckedItems property returns all checked items in a CheckedBoxList.  The following code snippet loops through all checked items and adds item contents to a StringBuilder and displays in a MessageBox.

Dim sb As New System.Text.StringBuilder

        For Each item In CheckedListBox1.CheckedItems

            sb.Append(item)

            sb.Append(" ")

        Next

        MessageBox.Show(sb.ToString())

 

Selection Mode

More coming soon ..



Summary

In this article, we discussed discuss how to create a CheckedListBox control in Windows Forms at design-time as well as run-ti After that, we saw how to use various properties and methods.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Mahesh, how can I add a Select All option(right above all other entries, as in Excel filtering). Once this option is checked then all entries must be checked, I am getting my values form SQL Server

Posted by taga Magadzire Apr 23, 2012

Hi Mahesh, Liked your article on CheckedListBox very much. Can you please further extend this article and tell us something about how to bind a CheckedListBox control to a datatable in a Database such as Access 2007 ??? Appreciate you very much! Best Regards, Maneesh Massey, maneeshmassey@gmail.com

Posted by Maneesh Feb 28, 2012

Tan - seriously...not necessary. Grow up and post on someone else's blog, that is if you have anything of any use to say. Thank you. Don't waste this helpful man's time.

Posted by Cory Owensby May 02, 2011

Sir, Thank you for sharing your knowledge. If I could ask for maybe a little more... I'm trying to set up this checked list box to send the values of multiple selections to a single field in a table together seperated by a comma. I think I understand that it needs a loop, but I'm not really sure how to code it. Can you help?

Posted by Cory Owensby May 02, 2011

CheckedItems represents all checked items in the list. The SeletedItems represents all items that are selected (doesn't matter if they have checked box or not).

Posted by Mahesh Chand Apr 24, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor