简体   繁体   English

在动态列表框中如何获取多个选择项?

[英]In dynamic listbox how to get multiple selection item?

private void Form1_Load(object sender, EventArgs e)
{
    int x = 0, y = 0;
    var list = GetFilterItems().Select(g => g.GroupID).Distinct();
    ListBox lst;
    foreach (var item in list)
    {
        lst  = new ListBox();
        lst.Size = new Size(161, 82);
        lst.Name = item.ToString();
        lst.SelectionMode = SelectionMode.MultiExtended;

        lst.Location = new Point(x,y+25);

        pnlFilters.Controls.Add(lst);
        lst.Click += new EventHandler(lst_Click);
        x += lst.Width+5;
    }             
}

void lst_Click(object sender, EventArgs e)
{           
    ItemChanged(sender);
}

private void ItemChanged(object sender)
{
    // Get the selected ListBox
    ListBox selectedListViewControl = (ListBox)sender;

    // Get the ListBox's items
    List<FilterItem> currentFilterItems = selectedListViewControl.Items.Cast<FilterItem>().ToList();

    // Get the ListBox's selected items
    List<FilterItem> selectedFilterItems = selectedListViewControl.SelectedItems.Cast<FilterItem>().ToList();
}

In above code i got only 1 selected item even i select multiple item from listbox how to get multiple item form listbox 在上面的代码中,即使我从列表框中选择了多个项目,我也只有1个选定的项目。

just create one listbox, then add all items to this single one 只需创建一个列表框,然后将所有项目添加到该列表框中

then the multi selection works 然后多重选择起作用

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

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