且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除列表框中的所有项目?

更新时间:2023-11-28 18:34:34

是不一样的在WinForm和网络表单的方式

  listBox1.Items.Clear()?; 


I created two RadioButton (Weight and Height). I will do switch between the two categories. But the they share the same ListBox Controllers (listBox1 and listBox2).

Is there any good method to clear all the ListBox items simpler? I didn't find the removeAll() for ListBox. I don't like my complex multi-lines style which I posted here.

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

isn't the same as the Winform and Webform way?

listBox1.Items.Clear();