Checked List Box

  • Thread starter Thread starter Harshil
  • Start date Start date
H

Harshil

Hi All,
I am working with the checked list box control in VB.NET. I am
displaying the names of the employees (display member) in the list box
and storing id as value member. Now i want to get all the employees how
have been selected (checked=true) from the checked list box. So I want
the ID of all the employees selected from the list box.

Any help would be appreciated

Thanks
 
Hi Harshil,
If you are using DisplayMember and ValueMembers of the list control it means
that each item in the checked-list box is object of type that has say Name
and ID properties.
Let for simplicity each item is of type DataItem

class DataItem
{
public int ID
{
get{...}
}

public string Name
{
get{....}
}
}

CheckedListBox control has a property called CheckedItems. Using that
property you can do

foreach(DataItem item in checkedListBox.CheckedItems)
{
//Prints the ID of checked items
Console.WriteLine( item.ID );

}

Because CheckedItems contoains all items which are in checked or in
indeterminate state you may be interested only of those that are checked.
Then you have to do more work to filter the items using CheckedIndices
collection insted.

foreach(int index in checkedListBox.CheckedIndices)
{
//Prints the ID of checked items
if(checkedListBox.GetItemCheckState(index) == CheckState.Checked)
{

Console.WriteLine( ((DataItem)checkedListBox.Items[index]).ID );
}

}

Finally if your control contains objects of different type you may use *as*
or *is* operators to find out and cast to if you already know the types of
the objects you may have. Or you can use reflection as list controls do to
get the value of the property used as ValueMember.

HTH
B\rgds
100
 
Back
Top