CheckedListBox in VB.NET

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
  • 73588

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.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.