views:

307

answers:

4

Hi, 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");
    }
+7  A: 

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

listBox1.Items.Clear();
balexandre
This is a poor answer because you failed to explain that he should not be using `Items.Add` that way in the first place. See Matt Dearning's answer for a much better solution.
Ray Burns
the question is "Is there any good method to clear all the ListBox items simpler?" it's a simpler question, it has a simple answer! Matt answer is for the question "Am I doing the right thing here?"
balexandre
It comes down to a sense of community responsibility. Consider: An eight-year-old comes up to me holding a loaded gun (not a toy). He clearly isn't paying attention to where he's pointing it. He asks me how to make it shoot. In this case "just pull this trigger" is a poor answer. A better answer would be to explain gun safety. For an eight year old the best answer would probably be to take the gun away and go find his parents. The stakes are much lower here but the principle is the same: **Encouraging poorly written code causes real damage and real pain.** It has a real cost to all of us.
Ray Burns
+1 Awesome comment Ray!!! You're right. Answering in such a way would also be helpful when somebody browses SO for answers.
Veer
+1  A: 

You should be able to use the Clear() method.

dkirk
+1  A: 

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

DancesWithBamboo
+4  A: 

I think it would be better to actually bind your listBoxes to a datasource, since it looks like you are adding the same elements to each listbox. A simple example would be something like this:

    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;
    }
Matt Dearing
you can even do in 1 line: listBox1.ItemsSource = listBox2.ItemsSource = _weight; :)
balexandre