How to get selected CheckedListBox items to list <X> ...?

I have a list of type X. X is a property level class. Now in the event, I need the CheckedListBox to select items in another list.

How to get a way out ... ?? The code I tried is 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...??? } 
+7
source share
5 answers

you can try the following

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

or cast as an object

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

I got an answer

 private void Click_Event(object sender, EventArgs e) { List<X> x2 = new List<X>(); foreach (X item in chkList.CheckedItems) { x2.Add(item); } } 
0
source

Here is a way that works for me:

 List<X> x2 = new List<X>(); x2 = chkList.CheckedItems.Cast<X>().ToList(); 
0
source
 string[] miList = chkList.CheckedItems.OfType<object>().Select(li => li.ToString()).ToArray(); 
0
source

This is another option.

 List<X> lst = new List<X>(chkList.CheckedItems.Cast<X>()); 
0
source

All Articles