C# Checked ListBox Control

The CheckedListBox control gives you all the capability of a list box and also allows you to display a check mark next to the items in the list box.

C# checkedlistbox

The user can place a check mark by one or more items and the checked items can be navigated with the CheckedListBox.CheckedItemCollection and CheckedListBox.CheckedIndexCollection .

Checkedlistbox add items

Syntax
public int Add (object item, bool isChecked);

You can add individual items to the list with the Add method . The CheckedListBox object supports three states through the CheckState enumeration: Checked, Indeterminate, and Unchecked.

checkedListBox1.Items.Add("Sunday", CheckState.Checked); checkedListBox1.Items.Add("Monday", CheckState.Unchecked); checkedListBox1.Items.Add("Tuesday", CheckState.Indeterminate);

C# checkedlistbox add item

If you want to add objects to the list at run time, assign an array of object references with the AddRange method . The list then displays the default string value for each object.

string[] days = new[] { "Sunday", "Monday", "Tuesday" }; checkedListBox1.Items.AddRange(days);

C# checkedlistbox run time

By default checkedlistbox items are unchecked .

Check all items in a Checkedlistbox

If you want to check an item in a Checkedlistbox, you need to call SetItemChecked with the relevant item.

public void SetItemChecked (int index, bool value);
Parameters
  1. index(Int32) - The index of the item to set the check state for.
  2. value(Boolean) - true to set the item as checked; otherwise, false.

If you want to set all items in a CheckedListBox to checked, change the value of SetItemChecked method to true.

string[] days = new[] { "Sunday", "Monday", "Tuesday" }; checkedListBox1.Items.AddRange(days); for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i, true); }

Uncheck all items in a Checkedlistbox

If you want to set all items in a CheckedListBox to unchecked, change the value of SetItemChecked method to false.

checkedListBox1.Items.Add("Sunday", CheckState.Checked); checkedListBox1.Items.Add("Monday", CheckState.Checked); checkedListBox1.Items.Add("Tuesday", CheckState.Checked); for (int i = 0; i < checkedListBox1.Items.Count; i++) { checkedListBox1.SetItemChecked(i, false); }

C# checkedlistbox unchecked

CheckedListBox DataSource

Following example shows how to bind a DataSource to CheckedListBox

C# checkedlistbox unchecked
DataTable dt = new DataTable(); DataColumn dc = new DataColumn("StringType", typeof(String)); dt.Columns.Add(dc); DataRow dr = dt.NewRow(); dr[0] = "Item_1"; dt.Rows.Add(dr); this.checkedListBox1.DataSource = dt; this.checkedListBox1.DisplayMember = "StringType";

How to get value of checked item from CheckedListBox?

public System.Windows.Forms.CheckedListBox.ObjectCollection Items { get; }

C# checkedlistbox datasource

The following example uses the Items property to get the CheckedListBox.ObjectCollection to retrieve the index of an item using the ListBox.ObjectCollection.IndexOf method.

private void button1_Click_1(object sender, EventArgs e) { foreach (int indexChecked in checkedListBox1.CheckedIndices) { // The indexChecked variable contains the index of the item. MessageBox.Show("Index: " + indexChecked.ToString() + ", is checked. Checked state is:" + checkedListBox1.GetItemCheckState(indexChecked).ToString() + "."); } }