How to get the CheckedListBox Selected Items into List<X>...?

Ravishankar N picture Ravishankar N · Dec 19, 2012 · Viewed 25.4k times · Source

I am having a List of type X. X is a Property Level Class. Now on an event i need the CheckedListBox Selected Items into another List.

How to get the output...?? The code i tried is given below...

public void Initialize(List<X> x1)
{
        chkList.DataSource = x1;
        chkList.DisplayMember = "MeterName"; // MeterName is a property in Class X
        chkList.ValueMember = "PortNum"; // PortNum is a property in Class X
}

private void Click_Event(object sender, EventArgs e)
{

List<X> x2 = new List<X>();
// Here I want to get the checkedListBox selected items in x2;
// How to get it...???

}

Answer

COLD TOLD picture COLD TOLD · Dec 19, 2012

you can try the following

 List<X>  x2 =  chkList.CheckedItems.OfType<X>().ToList();

or cast as object

List<object>  x2 = chkList.CheckedItems.OfType<object>().ToList();