2010-04-09 109 views
30

我创建了两个RadioButton(重量和高度)。我会在两个类别之间切换。但他们共享相同的ListBox控制器(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()方法。 – 2010-04-10 00:46:44

回答

70

是不是跟Winform和Webform的方式一样?

listBox1.Items.Clear(); 
+0

这是一个不好的答案,因为你没有解释他不应该首先使用'Items.Add'。请参阅Matt Dearning的答案,以获得更好的解决方案。 – 2010-04-11 07:41:36

+2

的问题是“有没有什么好方法可以简化所有ListBox项目?”这是一个更简单的问题,它有一个简单的答案!马特的回答是针对这个问题:“我在这里做正确的事情吗?” – balexandre 2010-04-11 12:15:47

+15

归结为社区责任感。想想看:一个八岁的孩子拿着一把装满枪的枪(不是玩具)向我走来。他显然没有注意他指向的地方。他问我如何拍摄。在这种情况下,“只是拉这个触发器”是一个不好的答案。更好的答案是解释枪支安全。对于一个八岁的孩子来说,最好的答案可能是拿走枪并去找他的父母。这里的风险要低得多,但原则是一样的:**鼓励写得不好的代码会导致真正的损害和真正的痛苦。**这对我们所有人都是一个真正的代价。 – 2010-04-12 17:55:56

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

这实质上就是ListBox.Items.Clear()所做的事情吗? – Josh 2012-12-27 15:39:21

8

我认为最好是将listBoxes绑定到数据源,因为它看起来像是在每个列表框中添加了相同的元素。一个简单的例子是这样的:

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

你甚至可以在1行:listBox1.ItemsSource = listBox2.ItemsSource = _weight; :) – balexandre 2010-04-10 00:45:51

2

写在cs文件下面的代码:

ListBox.Items.Clear();

0

我就这样做,和正常工作对我说:

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

     } 

解释:使用“清除()”只擦除的项目,不 从对象然后删除,使用RemoveAt移除()移除起始位置 的项目只是realocate别人[如果u删除项[0],[1]变为[0]触发一个新的内部事件], 所以从结局没有影响去其他位置, 它是一个取出堆栈行为,这种方式我们可以堆栈所有项目,重新设置对象。

+0

我不明白你为什么要问清理listBox,但你的答案是使用listview。 – Burgo855 2017-03-01 10:44:16

0
  • VB ListBox2.DataSource =无
  • C# ListBox2.DataSource = NULL;