首页 文章

在c#中获取ListBox中多个所选项的索引

提问于
浏览
2

我有两个ListBoxes . 首先 ListBox 项目是"Products"的列表 . 和第二个 ListBox 项目是"Item in Product"的列表所以当用户单击第一个(产品)列表框中的项目时,第二个 ListBox 将显示所选产品中的项目列表 .

例如:

Products     Items in Proucts
  AA*                 1
  BB                  2
  CC                  3

在上面的例子当前用户选择AA产品 . 1,2,3是产品AA中的项目 .

对于当前的计划,我已经完成了 . 用户 only can select One "Products" at a time . 然后我想 change to multipleselected. 所以我想得到用户选择的每个产品的索引号,然后我可以从数据库中检索数据以获得所有选定产品的"Items In Products" .

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

3 回答

  • 3
    if (productsListBox.SelectedItmes.Count >= 0)
    {
    
        string IDs = string.Empty;
        foreach( ListItem li in productsListBox.SelectedItmes ) 
        {
            IDs += li.Value+"," ;
        }
            IDs = IDs.Trim(',');
    
    }
    

    它会为您提供所选ID的CSV

  • 1

    我已经得到了答案:

    if (productListBox.SelectedItems.Count >= 0)
     {
        for (int i = 0; i < productListBox.SelectedItems.Count; i++)
           {
                MessageBox.Show(productListBox.SelectedIndices[i].ToString());
           }
      }
    
  • 0
    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;
        }
    

相关问题