繁体   English   中英

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

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

我有两个列表框。 第一个ListBox项目是“产品”的列表。 第二个ListBox项是“产品中的项”的列表,因此当用户单击第一个(产品)Listbox中的项时,第二个ListBox将显示所选“产品”中的项列表。

例如:

Products     Items in Proucts
  AA*                 1
  BB                  2
  CC                  3   

在上述示例中,当前用户选择了AA产品。 1,2,3是产品AA中的项目。

对于当前程序,我已经完成。 用户一次只能选择一个“产品” 然后,我想更改为多选。 因此,我想获取用户选择的每种产品的索引号,然后可以从数据库中检索数据以获取所有所选产品的“产品中的物品”。

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

我已经得到了答案:

 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(',');

}

它会为您提供所选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