Opt In (Do Not Edit Here)

Jun 1, 2015

Add, Remove and Select an item from CheckedListBox control

Prerequisites: Basic of C#, Windows Form

You'll get Answers for:
  • How to add item to checkedListBox using C#.
  • How to remove item from checkedListBox using C#.
  • How to select item from checkedListBox using C#.
 
Controls used:


 CheckedListBox Control
 clbList
 Button Control
 btnAdd, btnRemove
 TextBox Control
 txtElement
 Label Control
 lblValue, lblList

 
Description:

About CheckedListBox control...
A checkedListBox control has capability of ListBox control and CheckBox Control. This control contain serveral items in a list, with a checkbox next to each item in the list.

We can dynamically add item to the CheckedListBox control. Also we can Remove item or Select item programmatically from list.
The collection of items in this control has zero-based indexing.
To select an item in the list, the user can check the box on its left or can click the string to be selected.
To deselect the item, the user can simply uncheck the check mark in the box.

The Functions used...
 

ADD an Item :  public int Add(object item)
 

 This method is use to add an item to the list of items in CheckedListBox control.
 An object representing the item used to add to the collection.

 Here we are accepting values from user (using TextBox Control) to the CheckedListbox control.



if (txtElement.Text.Trim().Length != 0)
           clbList.Items.Add(txtElement.Text);
else
           MessageBox.Show("Enter Element Value...");


Listing: 1 (code for btnAdd's onClick() Event)

 In above code snippet, we first check the lenth of string or item enter by user, if item length is greater than 0 (i.e. No Blank Item) then we add the same to the 'clbList' using Add() function.
 
Add an Item to CheckedListBox Control
Figure 1: Add an item to the list

REMOVE an Itempublic void Remove(object value) OR public void RemoveAt(int index)
 These functions are used to remove the item from the list of items in CheckedListBox Control.
 An object representing the item to be removed from the collection.
 An index represents the item's index number in the list, zero-based indexing.


if (clbList.CheckedItems.Count != 0)
          for (int i = clbList.CheckedItems.Count; i > 0; i--)
                      clbList.Items.RemoveAt(clbList.CheckedIndices[i - 1]);
else
          MessageBox.Show("Element(s) Not Selected...");

Listing: 2 (code for btnRemove's onClick() Event)


 In above code snippet, we count no. of total items checked to be removed from the list, then if it is greater than 0, we remove the corresponding items from list. using RemoveAt() function.


before remove an item fromcheckedlistbox
Before Removing Item

after remove an item fromcheckedlistbox
After Removing Item
 Figure 2: Remove Item(s) from the list



SELECT an Item : public CheckedListBox.CheckedItemCollection CheckedItems { get; }

 To select the item, we use
CheckedListBox.CheckedItemCollection collection, which contains Collection of checked items in this System.Windows.Forms.CheckedListBox.
   

foreach (string item in clbList.CheckedItems)
          lblList.Text += "\n " + item;

Listing: 3 (code for clbList's SelectedIndexChanged() Event)

we can display each checked item by using simple foreach loop for all the checked items in CheckedItems collection of CheckedListBox control. 

select an item fromcheckedlistbox
 Figure 3: Select checked items from the list


Summary:
At the end we learnt to Add an Item, Remove and Select the item from CheckedListBox control using C#.

About The Author :

Freelancer and IT Engineer
Softwares Developed by Mayur Dighe ImmortalDotNet.WordPress.com

0 comments :

All Rights Reserved. 2014 Copyright SIMPLITONA

Powered By Blogger | Published By Gooyaabi Templates Designed By : BloggerMotion

Top