Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

C# Checked ListBox Control



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

The user can place a checkmark 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);

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);

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);
}

CheckedListBox DataSource

Following example shows how to bind a DataSource to CheckedListBox

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 the value of the checked item from CheckedListBox?

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

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() + ".");
    }
}

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More