简体   繁体   中英

insert All Listbox items into a List

Are there a way to export the items from an Listbox into a List in c#? i have Listbox1 that Contain some string items and I have This list:

public List<String> MyList = new List<String>();

how to add this items to Mylist?

You could do a casting like this:

public List<String> MyList = listBox1.Items.Cast<string>().ToList();

This way, to change the ListBox ObjectCollection to list of string.

You can use AddRange method:

If you are sure that all elements in Listbox1 are strings:

MyList.AddRange(ListBox1.Items.Cast<string>());

if not:

MyList.AddRange(ListBox1.Items.OfType<string>());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM