简体   繁体   English

在C#中获取列表框中多个选定项目的索引

[英]Getting index for multiple selected item in ListBox in c#

I have two ListBoxes. 我有两个列表框。 First ListBox items are list of "Products". 第一个ListBox项目是“产品”的列表。 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. 第二个ListBox项是“产品中的项”的列表,因此当用户单击第一个(产品)Listbox中的项时,第二个ListBox将显示所选“产品”中的项列表。

eg: 例如:

Products     Items in Proucts
  AA*                 1
  BB                  2
  CC                  3   

in example above current user selected AA products. 在上述示例中,当前用户选择了AA产品。 And 1,2,3 are the items in product AA. 1,2,3是产品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 它会为您提供所选ID的CSV文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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