如何删除所有ListBox项目?

36

我创建了两个单选按钮(Weight和Height),我希望在这两个类别之间切换。但是它们共享同一个ListBox Controllers(listBox1和listBox2)。

是否有更简便的方法清除所有ListBox项目?我没有找到ListBox的removeAll()方法。我不喜欢我在这里发布的复杂多行样式。

private void Weight_Click(object sender, RoutedEventArgs e)
    {
        // switch between the radioButton "Weith" and "Height"
        // Clear all the items first
        listBox1.Items.Remove("foot"); 
        listBox1.Items.Remove("inch");
        listBox1.Items.Remove("meter");
        listBox2.Items.Remove("foot");
        listBox2.Items.Remove("inch");
        listBox2.Items.Remove("meter");

        // Add source units items for listBox1
        listBox1.Items.Add("kilogram");
        listBox1.Items.Add("pound");

        // Add target units items for listBox2
        listBox2.Items.Add("kilogram");
        listBox2.Items.Add("pound");
    }

    private void Height_Click(object sender, RoutedEventArgs e)
    {
        // switch between the radioButton "Weith" and "Height"
        // Clear all the items first
        listBox1.Items.Remove("kilogram");
        listBox1.Items.Remove("pound");
        listBox2.Items.Remove("kilogram");
        listBox2.Items.Remove("pound");

        // Add source units items for listBox1
        listBox1.Items.Add("foot");
        listBox1.Items.Add("inch");
        listBox1.Items.Add("meter");

        // Add target units items for listBox2
        listBox2.Items.Add("foot");
        listBox2.Items.Add("inch");
        listBox2.Items.Add("meter");
    }

2
我非常喜欢Matt Dearing的答案。它似乎是针对这个特定用途更好的编程技术,而不仅仅是执行Items.Clear()方法。 - Mark Rejhon
7个回答

92

这不是和Winform和Webform的方式相同吗?

listBox1.Items.Clear();

这是一个糟糕的答案,因为你没有解释他不应该在第一时间使用Items.Add。请参考Matt Dearning的答案,那是一个更好的解决方案。 - Ray Burns
4
问题是:“有没有更简单的方法清除所有ListBox项?”这是一个简单的问题,有一个简单的答案!Matt的回答是针对问题“我这样做对吗?” - balexandre
17
这涉及到社区责任感。请考虑以下情境:一个八岁的孩子向我走来,手持一支装有子弹的枪(不是玩具枪)。他明显没有注意指向哪里。他问我如何让它射击。在这种情况下,“只需扣动扳机”是一个很糟糕的答案。更好的回答应该是解释枪支安全。对于一个八岁的孩子来说,最好的回答可能是把枪拿走并找到他的父母。这里的风险要低得多,但原则是相同的:鼓励编写糟糕的代码会造成真正的损失和真正的痛苦。这对我们所有人都有真正的代价。 - Ray Burns

9

我认为最好将您的listBoxes绑定到数据源,因为看起来您正在向每个listbox添加相同的元素。一个简单的示例可能是这样的:

    private List<String> _weight = new List<string>() { "kilogram", "pound" };
    private List<String> _height = new List<string>() { "foot", "inch", "meter" };

    public Window1()
    {            
        InitializeComponent();
    }        

    private void Weight_Click(object sender, RoutedEventArgs e)
    {
        listBox1.ItemsSource = _weight;
        listBox2.ItemsSource = _weight;
    }

    private void Height_Click(object sender, RoutedEventArgs e)
    {
        listBox1.ItemsSource = _height;
        listBox2.ItemsSource = _height;
    }

1
你甚至可以在一行代码中完成:listBox1.ItemsSource = listBox2.ItemsSource = _weight; :) - balexandre

3
在.cs文件中写入以下代码:
ListBox.Items.Clear();
该代码用于清空ListBox中的所有项。

2
while (listBox1.Items.Count > 0){ 
    listBox1.Items.Remove(0);
}

2
这个方法是否本质上就是清空 ListBox 的项目? - Josh

1

你应该能够使用 Clear() 方法。


0

我是这样写的,而且对我来说正常工作:

if (listview1.Items.Count > 0)
        {
            for (int a = listview1.Items.Count -1; a > 0 ; a--)
            {
                listview1.Items.RemoveAt(a);
            }
                listview1.Refresh();

        }

解释:使用“Clear()”仅清除项目,而不会将其从对象中删除。使用“RemoveAt()”从开始位置删除项目只是重新分配其他项目[如果您删除item[0],则item[1]变为[0]会触发新的内部事件],因此从末尾删除不影响其他位置,这是一种堆栈行为,这样我们可以堆叠所有项目,并重置对象。

我不明白为什么你在问如何清除listBox,但是你的答案却使用了listview。 - Burgo855

0
  • VB ListBox2.DataSource = Nothing
  • C# ListBox2.DataSource = null;
  • VB ListBox2.DataSource = 空
  • C# ListBox2.DataSource = null;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接