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

.NET programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

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

Post by Neo » Sun Apr 18, 2010 11:43 pm

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
    } 
}
User avatar
Herath
Major
Major
Posts: 417
Joined: Thu Aug 05, 2010 7:09 pm

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

Post by Herath » Tue Jul 19, 2011 11:03 pm

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
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

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

Post by Neo » Tue Jul 19, 2011 11:09 pm

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.
Post Reply

Return to “.NET Programming”