CheckListBox Properties, Events, and Methods Examples
Here are some examples of properties, events, and methods of the CheckedListBox control in Windows
Forms:
Properties:
1. CheckedItems: Gets a collection of the checked items in the CheckedListBox.
CheckedListBox.CheckedItemCollection checkedItems = checkedListBox1.CheckedItems;
2. CheckedIndices: Gets a collection of the zero-based indices of the checked items in
the CheckedListBox.
CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;
3. CheckState: Gets or sets the check state of an item.
CheckState itemCheckState = checkedListBox1.GetItemCheckState(0); // Get the check state of
the first item
checkedListBox1.SetItemCheckState(0, CheckState.Checked); // Set the check state of the first
item to Checked
Events:
1. ItemCheck: Occurs when the checked state of an item is about to change.
checkedListBox1.ItemCheck += CheckedListBox_ItemCheck;
private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
// Handle the item check event
2. ItemChecked: Occurs after the checked state of an item has changed.
checkedListBox1.ItemChecked += CheckedListBox_ItemChecked;
private void CheckedListBox_ItemChecked(object sender, ItemCheckedEventArgs e)
// Handle the item checked event
Dr. Ahmed Alnasheri 1/11
Methods:
1. SetItemChecked: Sets the checked state of an item.
checkedListBox1.SetItemChecked(0, true); // Checks the first item
2. GetItemChecked: Gets the checked state of an item.
bool isChecked = checkedListBox1.GetItemChecked(0); // Gets the checked state of the first
item
3. ClearSelected: Clears the selected item in the CheckedListBox.
checkedListBox1.ClearSelected();
4. SetItemCheckState: Sets the check state of an item using the CheckState enumeration.
checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate); // Sets the check state of the
first item to Indeterminate
These are just a few examples of the properties, events, and methods available in
the CheckedListBox control. You can explore more options and combinations to suit your application's
requirements.
Here are some additional properties, events, and methods of the CheckedListBox control in Windows
Forms:
Properties:
1. Items: Gets or sets the collection of items in the CheckedListBox.
CheckedListBox.ObjectCollection items = checkedListBox1.Items;
2. CheckedItemsCount: Gets the number of checked items in the CheckedListBox.
int checkedItemsCount = checkedListBox1.CheckedItemsCount;
3. CheckedIndicesCount: Gets the number of checked indices in the CheckedListBox.
int checkedIndicesCount = checkedListBox1.CheckedIndicesCount;
4. CheckOnClick: Gets or sets a value indicating whether the item is checked when clicked.
checkedListBox1.CheckOnClick = true; // Enable check on click behavior
Events:
1. SelectedIndexChanged: Occurs when the selected index in the CheckedListBox has changed.
checkedListBox1.SelectedIndexChanged += CheckedListBox_SelectedIndexChanged;
private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
Dr. Ahmed Alnasheri 2/11
// Handle the selected index changed event
2. ItemSelectionChanged: Occurs when an item's selection state changes.
checkedListBox1.ItemSelectionChanged += CheckedListBox_ItemSelectionChanged;
private void CheckedListBox_ItemSelectionChanged(object sender,
ListViewItemSelectionChangedEventArgs e)
// Handle the item selection changed event
Methods:
1. GetItemCheckState: Gets the check state of an item as a CheckState enumeration.
CheckState itemCheckState = checkedListBox1.GetItemCheckState(0); // Get the check state of
the first item
2. GetItemText: Gets the text representation of an item.
string itemText = checkedListBox1.GetItemText(0); // Get the text of the first item
3. GetItemBounds: Gets the bounding rectangle for an item.
Rectangle itemBounds = checkedListBox1.GetItemBounds(0); // Get the bounding rectangle of
the first item
4. SetItemText: Sets the text of an item.
checkedListBox1.SetItemText(0, "New Text"); // Set the text of the first item to "New Text"
These additional properties, events, and methods provide further flexibility and functionality when
working with the CheckedListBox control in your Windows Forms application. Feel free to explore their
usage and adapt them to your specific requirements.
Here are some more properties, events, and methods of the CheckedListBox control in Windows Forms:
Properties:
1. SelectionMode: Gets or sets the selection mode of the CheckedListBox.
checkedListBox1.SelectionMode = SelectionMode.MultiExtended; // Enable multiple extended
selection mode
2. DisplayMember: Gets or sets the property to display for each item in the CheckedListBox.
checkedListBox1.DisplayMember = "Name"; // Set the display member to the "Name" property
Dr. Ahmed Alnasheri 3/11
of each item
3. ValueMember: Gets or sets the property to use as the actual value for each item in
the CheckedListBox.
checkedListBox1.ValueMember = "ID"; // Set the value member to the "ID" property of each
item
4. CheckOnClick: Gets or sets a value indicating whether the item is checked when clicked.
checkedListBox1.CheckOnClick = true; // Enable check on click behavior
Events:
1. ItemCheck: Occurs when the checked state of an item is about to change.
checkedListBox1.ItemCheck += CheckedListBox_ItemCheck;
private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
// Handle the item check event
2. ItemCheck: Occurs after the checked state of an item has changed.
checkedListBox1.ItemChecked += CheckedListBox_ItemChecked;
private void CheckedListBox_ItemChecked(object sender, ItemCheckedEventArgs e)
// Handle the item checked event
Methods:
1. ClearSelected: Clears the selected item in the CheckedListBox.
checkedListBox1.ClearSelected();
2. FindString: Searches for the first item in the CheckedListBox that starts with the specified string.
int index = checkedListBox1.FindString("SearchText"); // Find the index of the first item starting
with "SearchText"
3. GetItemCheckState: Gets the check state of an item as a CheckState enumeration.\
CheckState itemCheckState = checkedListBox1.GetItemCheckState(0); // Get the check state of
the first item
Dr. Ahmed Alnasheri 4/11
4. SetItemCheckState: Sets the check state of an item using the CheckState enumeration.
checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate); // Set the check state of the
first item to Indeterminate
These additional properties, events, and methods provide further control and customization options
when working with the CheckedListBox control in your Windows Forms application. Feel free to explore
their usage and adapt them to suit your specific requirements.
Here are some additional properties, events, and methods of the CheckedListBox control in Windows
Forms:
Properties:
1. CheckOnClick: Gets or sets a value indicating whether the item is checked when clicked.
checkedListBox1.CheckOnClick = true; // Enable check on click behavior
2. SelectionMode: Gets or sets the selection mode of the CheckedListBox.
checkedListBox1.SelectionMode = SelectionMode.MultiExtended; // Enable multiple extended
selection mode
3. CheckedItems: Gets a collection of the checked items in the CheckedListBox.
CheckedListBox.CheckedItemCollection checkedItems = checkedListBox1.CheckedItems;
4. CheckedIndices: Gets a collection of the zero-based indices of the checked items in
the CheckedListBox.
CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;
Events:
1. SelectedIndexChanged: Occurs when the selected index in the CheckedListBox has changed.
checkedListBox1.SelectedIndexChanged += CheckedListBox_SelectedIndexChanged;
private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
// Handle the selected index changed event
2. ItemCheck: Occurs when the checked state of an item is about to change.
checkedListBox1.ItemCheck += CheckedListBox_ItemCheck;
private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
Dr. Ahmed Alnasheri 5/11
{
// Handle the item check event
Methods:
1. SetItemChecked: Sets the checked state of an item.
checkedListBox1.SetItemChecked(0, true); // Checks the first item
2. GetItemChecked: Gets the checked state of an item.
bool isChecked = checkedListBox1.GetItemChecked(0); // Gets the checked state of the first
item
3. ClearSelected: Clears the selected item in the CheckedListBox.
checkedListBox1.ClearSelected();
4. SetItemCheckState: Sets the check state of an item using the CheckState enumeration.
checkedListBox1.SetItemCheckState(0, CheckState.Indeterminate); // Sets the check state of the
first item to Indeterminate
These additional properties, events, and methods provide further control and functionality when
working with the CheckedListBox control in your Windows Forms application. Feel free to explore their
usage and adapt them to suit your specific requirements.
Here are some more properties, events, and methods of the CheckedListBox control in Windows Forms:
Properties:
1. Items: Gets or sets the collection of items in the CheckedListBox.
CheckedListBox.ObjectCollection items = checkedListBox1.Items;
2. CheckedItemsCount: Gets the number of checked items in the CheckedListBox.
int checkedItemsCount = checkedListBox1.CheckedItemsCount;
3. CheckedIndicesCount: Gets the number of checked indices in the CheckedListBox.
int checkedIndicesCount = checkedListBox1.CheckedIndicesCount;
4. DisplayMember: Gets or sets the property to display for each item in the CheckedListBox.
checkedListBox1.DisplayMember = "Name"; // Set the display member to the "Name" property
of each item
Events:
Dr. Ahmed Alnasheri 6/11
1. ItemSelectionChanged: Occurs when an item's selection state changes.
checkedListBox1.ItemSelectionChanged += CheckedListBox_ItemSelectionChanged;
private void CheckedListBox_ItemSelectionChanged(object sender,
ListViewItemSelectionChangedEventArgs e)
// Handle the item selection changed event
2. MeasureItem: Occurs when the size of an item is required.
checkedListBox1.MeasureItem += CheckedListBox_MeasureItem;
private void CheckedListBox_MeasureItem(object sender, MeasureItemEventArgs e)
// Handle the measure item event
Methods:
1. GetItemCheckState: Gets the check state of an item as a CheckState enumeration.
CheckState itemCheckState = checkedListBox1.GetItemCheckState(0); // Get the check state of
the first item
2. GetItemText: Gets the text representation of an item.
string itemText = checkedListBox1.GetItemText(0); // Get the text of the first item
3. GetItemBounds: Gets the bounding rectangle for an item.
Rectangle itemBounds = checkedListBox1.GetItemBounds(0); // Get the bounding rectangle of
the first item
4. SetItemText: Sets the text of an item.
checkedListBox1.SetItemText(0, "New Text"); // Set the text of the first item to "New Text"
5. BeginUpdate and EndUpdate: Temporarily suspends the painting of the CheckedListBox until
the EndUpdate method is called.
checkedListBox1.BeginUpdate();
// Perform multiple operations on the CheckedListBox
checkedListBox1.EndUpdate();
Dr. Ahmed Alnasheri 7/11
These additional properties, events, and methods offer more customization and control over the
behavior and appearance of the CheckedListBox control in your Windows Forms application. You can
explore their usage and adapt them to meet your specific requirements.
Here's an example of how to use the CheckedListBox control's Items property to populate the control
with items programmatically:
using System;
using System.Windows.Forms;
namespace CheckedListBoxExample
public partial class Form1 : Form
public Form1()
InitializeComponent();
private void Form1_Load(object sender, EventArgs e)
// Populate the CheckedListBox with items
checkedListBox1.Items.Add("Item 1");
checkedListBox1.Items.Add("Item 2");
checkedListBox1.Items.Add("Item 3");
checkedListBox1.Items.Add("Item 4");
checkedListBox1.Items.Add("Item 5");
private void button1_Click(object sender, EventArgs e)
// Get the checked items
string checkedItems = string.Empty;
foreach (object item in checkedListBox1.CheckedItems)
Dr. Ahmed Alnasheri 8/11
checkedItems += item.ToString() + Environment.NewLine;
MessageBox.Show("Checked Items:" + Environment.NewLine + checkedItems);
In this example, we have a Form with a CheckedListBox control named checkedListBox1 and
a Button control named button1.
In the Form_Load event handler, we populate the checkedListBox1 control with items using
the Items.Add method.
In the button1_Click event handler, we iterate over the CheckedItems collection of
the checkedListBox1 control to obtain the checked items. We then display the checked items in a
message box.
When you run the application and check some items in the CheckedListBox, clicking the button1 button
will display a message box with the checked items.
This is a basic example of how to use the Items property to populate and retrieve items from
a CheckedListBox control programmatically. You can modify it to suit your specific needs.
Here are some additional details about the Items property of the CheckedListBox control:
The Items property of the CheckedListBox control represents the collection of items contained within
the control. It allows you to programmatically add, remove, and modify items in the CheckedListBox.
Adding Items:
You can add items to the CheckedListBox by using the Items.Add method. This method adds a new item
to the end of the item collection.
checkedListBox1.Items.Add("Item 1"); // Add a new item to the CheckedListBox
You can also add multiple items at once by passing an array or a collection to
the Items.AddRange method.
string[] itemsArray = { "Item 1", "Item 2", "Item 3" };
checkedListBox1.Items.AddRange(itemsArray); // Add multiple items to the CheckedListBox
Removing Items:
To remove an item from the CheckedListBox, you can use
the Items.Remove or Items.RemoveAt methods.
checkedListBox1.Items.Remove("Item 1"); // Remove a specific item by its value
checkedListBox1.Items.RemoveAt(0); // Remove an item at a specific index
Dr. Ahmed Alnasheri 9/11
You can also clear all items in the CheckedListBox by using the Items.Clear method.
checkedListBox1.Items.Clear(); // Remove all items from the CheckedListBox
Modifying Items:
To modify the text or value of an item in the CheckedListBox, you can directly access the item's
properties using the index.
checkedListBox1.Items[0] = "New Item 1"; // Change the text of the first item
You can also retrieve the text or value of an item using the index.
string itemText = checkedListBox1.Items[0].ToString(); // Get the text of the first item
Counting Items:
You can get the total number of items in the CheckedListBox using the Items.Count property.
int itemCount = checkedListBox1.Items.Count; // Get the total number of items in the CheckedListBox
These are some of the ways you can work with the Items property of the CheckedListBox control to add,
remove, and modify items programmatically. You can use these methods and properties to dynamically
manage the content of the CheckedListBox based on your application's requirements.
The CheckedListBox control in Windows Forms does not have an explicit "Value" property for its items.
However, you can associate custom data or values with each item using the Tag property or by creating
a custom class to represent the items.
Here are two approaches to retrieve the value of an item in a CheckedListBox:
1. Using the Tag Property:
The Tag property of each item in the CheckedListBox allows you to associate custom data or values with
the item. You can set the Tag property when adding items and retrieve the value later.
Example:
// Adding items with associated values
checkedListBox1.Items.Add("Item 1").Tag = 100;
checkedListBox1.Items.Add("Item 2").Tag = 200;
checkedListBox1.Items.Add("Item 3").Tag = 300;
// Retrieving the value of a checked item
if (checkedListBox1.CheckedItems.Count > 0)
object selectedItem = checkedListBox1.CheckedItems[0];
int value = (int)((ListBoxItem)selectedItem).Tag;
// Use the value as needed
Dr. Ahmed Alnasheri 10/11
}
In this example, we set the Tag property of each item to an associated value when adding the items.
Later, when retrieving the value of a checked item, we cast the Tag property back to the appropriate
data type (int in this case).
2. Using a Custom Class:
Another approach is to create a custom class that represents the items in the CheckedListBox. This
custom class can have properties such as Text and Value to store the item's display text and associated
value.
Example:
public class CheckedListBoxItem
public string Text { get; set; }
public int Value { get; set; }
// Adding items with associated values using the custom class
checkedListBox1.Items.Add(new CheckedListBoxItem { Text = "Item 1", Value = 100 });
checkedListBox1.Items.Add(new CheckedListBoxItem { Text = "Item 2", Value = 200 });
checkedListBox1.Items.Add(new CheckedListBoxItem { Text = "Item 3", Value = 300 });
// Retrieving the value of a checked item
if (checkedListBox1.CheckedItems.Count > 0)
CheckedListBoxItem selectedItem = (CheckedListBoxItem)checkedListBox1.CheckedItems[0];
int value = selectedItem.Value;
// Use the value as needed
In this example, we create a custom class CheckedListBoxItem with Text and Value properties. We then
add instances of this class to the CheckedListBox control. When retrieving the value of a checked item,
we cast the selected item to the CheckedListBoxItem type and access its Value property.
These approaches allow you to associate and retrieve custom values for items in
the CheckedListBox control. You can choose the approach that best fits your application's needs and
data structure.
Dr. Ahmed Alnasheri 11/11