Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...???

}
like image 726
Ravishankar N Avatar asked Dec 19 '12 05:12

Ravishankar N


2 Answers

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();
like image 134
COLD TOLD Avatar answered Oct 10 '22 10:10

COLD TOLD


Here is a way that works for me:

List<X> x2 = new List<X>();
x2 = chkList.CheckedItems.Cast<X>().ToList();
like image 40
Joe Sisk Avatar answered Oct 10 '22 10:10

Joe Sisk