简体   繁体   中英

Getting index for multiple selected item in ListBox in c#

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

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

private string GetTagsList()
    {
        string Tags = string.Empty;

        if (lstTags.SelectedItems.Count >= 0)
        {
            for (int i = 0; i < lstTags.SelectedItems.Count; i++)
            {
                Tags += lstTags.SelectedIndices[i].ToString() + ",";
            }
            Tags = Tags.Trim(',');
        }

        return Tags;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM