【问题标题】:Removing more than 1 item in listbox删除列表框中超过 1 项
【发布时间】:2014-01-22 11:10:51
【问题描述】:

请帮助我如何在listbox 中删除超过 1 项。我知道删除 1 项的代码:listbox.Items.RemoveAt(i) 但是对于在列表框中选择的超过 1 个项目,代码是什么?例如在按钮单击事件中编写代码。我的 winform 中只有一个按钮和一个列表框。(用 C# 编写代码)

【问题讨论】:

  • 只需循环遍历所选项目并将其删除

标签: c# listboxitems


【解决方案1】:
while(listbox.SelectedItems.Count > 0)
 {
    listbox.Items.Remove(listbox.SelectedItem);
 }

【讨论】:

  • 不错的开始,兄弟 :)
【解决方案2】:
ListBox1.ClearSelection();

    //or

    foreach (ListItem listItem in ListBox1.Items)
    {
        listItem.Selected = false;
    }


     List<ListItem> itemsToRemove = new List<ListItem>();
    foreach (ListItem listItem in ListBox1.Items)
    {
        if (listItem.Selected)
            itemsToRemove.Add(listItem);
    }

    foreach (ListItem listItem in itemsToRemove)
    {
        ListBox1.Items.Remove(listItem);
    }

【讨论】:

  • 正确的想法,但我想知道 ListItemlistItem.Selected 是什么?
猜你喜欢
  • 1970-01-01
  • 2021-01-19
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 2020-12-07
相关资源
最近更新 更多