Getting index for multiple selected item in ListBox in c#

Qusyaire Ezwan picture Qusyaire Ezwan · Jun 22, 2012 · Viewed 10.8k times · Source

I have two ListBoxes. First ListBox items are list of "Products". and second ListBox items are list of "Item in Product" so When user click the item in first(Product) Listbox The second ListBox will show the list of items in selected Products.

eg:

Products     Items in Proucts
  AA*                 1
  BB                  2
  CC                  3   

in example above current user selected AA products. And 1,2,3 are the items in product AA.

For the current program,i've done. User only can select One "Products" at a time. Then i want to change to multipleselected. So i want to get index number for each product that user selects, then i can retrieve data from database to get "Items In Products" for all selected products.

if (productsListBox.SelectedItmes.Count >= 0)
{
 // please provide me coding here to get index number for each selected items in   productListBox.
}

Answer

Qusyaire Ezwan picture Qusyaire Ezwan · Jun 22, 2012

i already getting the answer:

 if (productListBox.SelectedItems.Count >= 0)
 {
    for (int i = 0; i < productListBox.SelectedItems.Count; i++)
       {
            MessageBox.Show(productListBox.SelectedIndices[i].ToString());
       }
  }