2

我有两个列表框。第一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.
}
4

3 回答 3

3

我已经得到答案:

 if (productListBox.SelectedItems.Count >= 0)
 {
    for (int i = 0; i < productListBox.SelectedItems.Count; i++)
       {
            MessageBox.Show(productListBox.SelectedIndices[i].ToString());
       }
  }
于 2012-06-22T09:15:14.663 回答
1
if (productsListBox.SelectedItmes.Count >= 0)
{

    string IDs = string.Empty;
    foreach( ListItem li in productsListBox.SelectedItmes ) 
    {
        IDs += li.Value+"," ;
    }
        IDs = IDs.Trim(',');

}

它会为您提供所选 ID 的 CSV

于 2012-06-22T09:04:50.127 回答
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;
    }
于 2014-03-24T14:15:22.773 回答