Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Windows Forms » CheckedListBox in VB.NET

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 CheckedListBox control.

Author Rank :
Page Views : 10863
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
bad article...useless by Tan On April 21, 2011
13 years my arse...this is the best u can come out with...what did u do...work in the teaching environment...pathetic...
Reply | Email | Modify 
Re: bad article...useless by Mahesh On April 22, 2011
Alright! How many years have you worked in programming? Have you written any articles, blogs? Helped anyone? Shared anything? Let's see what you can come up with. It's very easy to post a comment but hard to help somebody else.
Reply | Email | Modify 
Sadly by Tan On April 22, 2011
Sad to say, no I have no blog nor articles, but I have helped people graduate from College. And with your explanation in this article, since u initialize this article, i would suggest you greatly to explain deeper. What you explain here, even my nephew(10years-true story) can handle. Explain deeper, touch the array...Unless you started this article just for the sake of putting ur picture on the net. PS://Don't create another negativity when your negativity is not yet solve.
Reply | Email | Modify 
Sadly by Tan On April 22, 2011
Sad to say, no I have no blog nor articles, but I have helped people graduate from College. And with your explanation in this article, since u initialize this article, i would suggest you greatly to explain deeper. What you explain here, even my nephew(10years-true story) can handle. Explain deeper, touch the array...Unless you started this article just for the sake of putting ur picture on the net. PS://Don't create another negativity when your negativity is not yet solve.
Reply | Email | Modify 
Re: Sadly by Mahesh On April 22, 2011
First of all, I am not here to teach you or anybody anything. This is my personal voice and I say, what I like and think. The Sad part is, teachers like you, copy code from person like me. I write what I want. No body pays me salary for that. I do this in my free time. If it helps anybody, fine, if not, also fine. YOU or no body else is paying me for that. On the other hand, you as a teacher should use YOUR BRAIN to write your own excercises then copy and learn from person like me. It's because teachers like you, students are probably have no knowledge.
Reply | Email | Modify 
...continue by Tan On April 22, 2011
and also, that is why i say 15 years of experience? and come out with this article...I am trying to show my students how vast diversity the net is and all i get is you, in the first page of google...if u are in my shoes, you would understand. anyway not to patronize you, but to show you what you can do with 15 years of EXPERIENCE...not KNOWLEDGE
Reply | Email | Modify 
funny by Tan On April 22, 2011
haha...teachers like me shares people experience to the world so that they can see different types of work published. Why help someone when its useless or pointless, you know who am I referring too. Look here, all I am just saying, with ur 13 years experience, write something better. Assuming someone is hiring you, and they go to the net and search for ur work, like this, and you tell me u have 13 years experience and you write like this, what am I gonna think. WHAT THE HELL HAVE YOU BEEN DOING FOR 13 years. And also I am trying to let you know this since Day 1, but you seem to not accept it. Too bad then!
Reply | Email | Modify 
Re: funny by Cory On May 2, 2011
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.
Reply | Email | Modify 
A little confusion by Dazz On April 24, 2011
Thanks for this article, it was really helpful to me. I am confused about one thing though- What is the difference between Checked Items and Selected Items? Thanks again, and keep up the good work :)
Reply | Email | Modify 
Re: A little confusion by Mahesh On April 24, 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).
Reply | Email | Modify 
Question by Cory On May 2, 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?
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.