Most succinct way to convert ListBox.items to a generic list

I am using C# and targeting the .NET Framework 3.5. I'm looking for a small, succinct and efficient piece of code to copy all of the items in a ListBox to a List<String> (Generic List).

At the moment I have something similar to the below code:

        List<String> myOtherList =  new List<String>();
        // Populate our colCriteria with the selected columns.

        foreach (String strCol in lbMyListBox.Items)

Which works, of course, but I can't help but get the feeling that there must be a better way of doing this with some of the newer language features. I was thinking of something like the List.ConvertAll method but this only applies to Generic Lists and not ListBox.ObjectCollection collections.

14.10.2009 10:35:50
What are the objects in the list box?
stevehipwell 14.10.2009 10:44:42
Sorry - should have explicitly stated that they were Strings.
jamiei 14.10.2009 10:56:13
an extension method is what I like
nawfal 5.07.2012 12:38:36
not the most succinct, but a variety of options here
nawfal 9.07.2012 18:32:25
Jim Fell 7.07.2016 18:26:44

A bit of LINQ should do it:-

 var myOtherList = lbMyListBox.Items.Cast<String>().ToList();

Of course you can modify the Type parameter of the Cast to whatever type you have stored in the Items property.

14.10.2009 10:51:34
Is List<String> Mylist = new List<String>(lbMyListBox.Items.Cast<String>()); better or it makes no difference? The question, I guess, is if, this way, you avoid the creation of another list or not.
ThunderGr 18.10.2013 09:24:14

How about this:

List<string> myOtherList = (from l in lbMyListBox.Items.Cast<ListItem>() select l.Value).ToList();
14.10.2009 10:44:41

What about:


EDIT based on comments and DavidGouge's answer:

myOtherList.AddRange(lbMyListBox.Items.Select(item => ((ListItem)item).Value));
14.10.2009 10:55:37
it will give the following error: cannot convert from 'System.Windows.Forms.ListBox.ObjectCollection' to 'System.Collections.Generic.IEnumerable<string>'
Wael Dalloul 14.10.2009 10:48:41
ObjectCollection is IEnumerable but not IEnumerable<T> which is required by AddRange.
AnthonyWJones 14.10.2009 10:50:47
this doesnt even compile for me
nawfal 9.07.2012 18:31:23

The following will do it (using Linq):

List<string> list = lbMyListBox.Items.OfType<string>().ToList();

The OfType call will ensure that only items in the listbox's items that are strings are used.

Using Cast, if any of the the items are not strings, you will get an exception.

14.10.2009 10:48:43
Of course by using OfType the resulting list may be missing items. If the set of items are expected to be of a specific type use Cast since it doesn't hide a bug where the wrong types have been added to the ListBox (which is easy to do). Additionaly OfType will skip items that have a Conversion operator to the expected output type whereas Cast will invoke the converter operator.
AnthonyWJones 14.10.2009 10:57:15
Yes. I should really have said "items that are not castable to strings". The OfType issue is a good point, but if the ListBox should only contain strings, it will stop a crash in the case where something else erroneously creeps in
adrianbanks 14.10.2009 11:05:00
I'm expecting the listbox to be filled with strings but in the case of a non-string object being placed in the listbox, I would rather silently skip that item than cause an exception and deal with that exception in much the same way.
jamiei 14.10.2009 11:24:13

You don't need more. You get List of all values from Listbox

private static List<string> GetAllElements(ListBox chkList)
            return chkList.Items.Cast<ListItem>().Select(x => x.Value).ToList<string>();
27.01.2015 16:37:51