add items to list<>

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello,
I am trying to add SelectedItems from ListBox to List<string>. It seems
like it's not working,any idea how to do it?
Thank you!
 
Assuming the items in the ListBox are strings, then just loop:

foreach (string item in lb.SelectedItems) {
list.Add(item);
}

The problem is that it doesn't know that each item is a string, since
it can be anything.

Marc
 
Note on .NET 3.5 /C# 3 you could use:

list.AddRange(listbox.SelectedIndices.Cast<string>());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top