Windows Forms 2.0 – CheckedListBox Control Recipes

Posted by: Suprotim Agarwal , on 3/13/2007, in Category WinForms & WinRT
Views: 26441
Abstract: In this article, we will explore how to perform some common tasks using the CheckedListBox Control of Windows Forms 2.0.
Windows Forms 2.0 – CheckedListBox Control Recipes
 
The System.Windows.Forms.CheckedListBox control extends the ListBox control. It is very similar to a ListBox, except that it displays a ListBox in which a check box is displayed to the left of each item in the list. Figure --- displays a CheckedListBox.
 
How Do I Get Started With The CheckedListBox Control
 
The System.Windows.Forms.CheckedListBox control extends the ListBox control. It is very similar to a ListBox, except that it displays a ListBox in which a check box is displayed to the left of each item in the list. Figure --- displays a CheckedListBox.
 
As with standard list boxes, you can use the Items property to access the items in a checked listbox. Checked list boxes can have items added at run time using the String Collection Editor or their items can be added dynamically from a collection at run time, using the Items property.
 
The following example illustrates how you can use the methods, properties, and collections of a CheckedListBox. The example starts by initializing a CheckedListBox and sets up the initial objects in an array. It then assigns the entire array to the Items collection using the AddRange() method.
 
An ItemCheck Event has been programmatically added. This event occurs when the checked state of an item changes. The NewValue member enables you to determine the new check state for the specified item before the check state is changed by the CheckedListBox control.
 
// Create a Class level variable
CheckedListBox cbList;
 
private void CreateCheckedListBox()
{
cbList = new CheckedListBox();
cbList.Location = new Point(30, 30);
string[] myFruit = { "Apples", "Oranges", "Tomato" };
cbList.Items.AddRange(myFruit);
cbList.ItemCheck += new ItemCheckEventHandler(cbList_ItemCheck);
Controls.Add(cbList);
}
 
 
private void cbList_ItemCheck(object sender, ItemCheckEventArgs e)
{
switch (e.NewValue )
{
case CheckState.Checked:
// insert your code
break;
case CheckState.Indeterminate:
// insert your code
break;
case CheckState.Unchecked:
// insert your code
break;
default:
break;
}
}
 
Note: While creating the switch block, you can make use of codesnippets to autocomplete the block. Press Tab twice after typing the switch keyword and watch the fun.
 
How Do I Determine The Checked Items In The CheckedListbox Control
 
You can determine which items are checked in two ways. Either iterate through the collection stored in the CheckedItems property, or step through the list using the GetItemChecked method. The GetItemChecked method takes an item index number as its argument and returns true or false. CheckedIndices and CheckedItems properties provide collections of checked items. The collection is a subset of the objects in the Items collection, representing only those items whose System.Windows.Forms.CheckState is Checked or Indeterminate.
The following example demonstrates using the CheckedItems property to get the CheckedListBox.CheckedItemCollection. A button (btnDispChk) has been placed on the form and the code has been invoked in its Click Event.
 
private void btnDispChk_Click(object sender, EventArgs e)
{
string strChkdItems = "The Checked Items are : ";
 
foreach (string strItem in cbList.CheckedItems)
{
strChkdItems += strItem + " ";
}
 
MessageBox.Show(strChkdItems);
 
 
 
}
 
How Do I Programatically Check/Uncheck All Items In The CheckedListBox
 
CheckedListBox.SetItemChecked Method sets CheckState for the item at the specified index to Checked.
 
This method accepts 2 parameters :
 
index - The index of the item to set the check state for.
value - When a value of true is passed, this method sets the CheckState value to Checked. A value of false sets CheckState to Unchecked.
 
The following example enumerates the items in the CheckedListBox and uses the GetItemChecked method to determine the specific state the item is in. If the value is true, SetItemChecked is called on the item to set the checked state to UnChecked, else it is set to Checked. A button (btnChkAll) has been added to the form. This example also assumes that all the items in a CheckedListBox are in the same state.
 
 
private void btChkAll_Click(object sender, EventArgs e)
{
for (int i = 0; i < cbList.Items.Count; i++)
{
if (cbList.GetItemChecked(i) == true)
{
cbList.SetItemChecked(i, false);
}
else
{
cbList.SetItemChecked(i, true);
}
}
 
}
Conclusion :
 
In this article, we saw a recipe approach to performing some of the most common tasks with the CheckedListBox Control. I hope the article was useful and I thank you for viewing it.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Bond on Tuesday, April 15, 2008 10:20 AM
Good Link for beginners
Comment posted by ukuta ken on Saturday, November 29, 2008 2:58 AM
simple. perfect
Comment posted by om on Wednesday, December 8, 2010 12:07 PM
Good one nice..........
Comment posted by shruthi on Wednesday, May 15, 2013 8:30 AM
a+2