【问题标题】:Getting index for multiple selected item in ListBox in c#在C#中获取ListBox中多个选定项的索引
【发布时间】:2012-06-22 09:01:09
【问题描述】:

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

例如:

Products     Items in Proucts
  AA*                 1
  BB                  2
  CC                  3   

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

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

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

【问题讨论】:

    标签: c# listbox multiple-select


    【解决方案1】:

    我已经得到答案了:

     if (productListBox.SelectedItems.Count >= 0)
     {
        for (int i = 0; i < productListBox.SelectedItems.Count; i++)
           {
                MessageBox.Show(productListBox.SelectedIndices[i].ToString());
           }
      }
    

    【讨论】:

    • SelectedIndices 应替换为 SelectedItems。试图建议编辑,但审阅者回答“此编辑不正确”。找出原因。
    【解决方案2】:
    if (productsListBox.SelectedItmes.Count >= 0)
    {
    
        string IDs = string.Empty;
        foreach( ListItem li in productsListBox.SelectedItmes ) 
        {
            IDs += li.Value+"," ;
        }
            IDs = IDs.Trim(',');
    
    }
    

    它会给你一个选定 ID 的 CSV

    【讨论】:

    • 我确实使用 VS 2005。看看这里不支持 ListItem 属性。
    • 有 ListViewItem :)。 MSDN
    • 好的。其实你这里没有提到web或windows,我以为你问的是web(asp.net)。
    • 尝试编辑错别字,但当我将 SelectedItmes 重命名为 SelectedItems 时被拒绝。
    【解决方案3】:
    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;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      相关资源
      最近更新 更多