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

时间:2012-12-21 13:16:58

标签: c# winforms

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();
}

在上面的代码中,即使我从列表框中选择了多个项目,我也只获得了1个选项 如何获取多个项目表单列表框

1 个答案:

答案 0 :(得分:0)

只需创建一个列表框,然后将所有项目添加到此单个列表

然后多选工作