Page 1 of 1

How to get multiple selected items from a listbox in C#

Posted: Sun Apr 18, 2010 11:43 pm
by Neo
You can use following code to do this.

Code: Select all

foreach (ListItem Item in ListBox.Items) { 
    if (Item.Selected == true) {
         // You can use Item.Text to get the selected text of each item
    } 
}

Re: How to get multiple selected items from a listbox in C#

Posted: Tue Jul 19, 2011 11:03 pm
by Herath
There is a "SelectedItems" property for the ListBox. It is going to be easier that way. :)

Code: Select all

foreach (Object i in listBox1.SelectedItems)
            {
                MessageBox.Show(i.ToString());
            }
http://msdn.microsoft.com/en-us/library ... items.aspx

Re: How to get multiple selected items from a listbox in C#

Posted: Tue Jul 19, 2011 11:09 pm
by Neo
Great. That's faster than the previous one for sure.
I was helping someone to do this and posted here for the use of others.
Nice to see you guys are finding better ways and improve contents.