Getting index for multiple selected item in ListBox in c#

11,068

Solution 1

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());
       }
  }

Solution 2

if (productsListBox.SelectedItmes.Count >= 0)
{

    string IDs = string.Empty;
    foreach( ListItem li in productsListBox.SelectedItmes ) 
    {
        IDs += li.Value+"," ;
    }
        IDs = IDs.Trim(',');

}

It'll give you a CSV of selected IDs

Share:
11,068
Qusyaire Ezwan
Author by

Qusyaire Ezwan

Updated on June 17, 2022

Comments

  • Qusyaire Ezwan
    Qusyaire Ezwan about 2 years

    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.
    }