Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all ListBox items?

Tags:

c#

wpf

listbox

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");     } 
like image 886
Nano HE Avatar asked Apr 09 '10 23:04

Nano HE


People also ask

How do you delete all items in a ListBox?

If we want to clear the items in the Listbox widget, we can use the delete(0, END) method. Besides deleting all the items in the Listbox, we can delete a single item as well by selecting an item from the Listbox, i.e., by using currselection() method to select an item and delete it using the delete() function.

How do I clear a ListBox in Visual Studio?

All information about the removed item is deleted. You can use this method to remove a specific item from the list by specifying the index of the item to remove from the list. To specify the item to remove instead of the index to the item, use the Remove method. To remove all items from the list, use the Clear method.

How do I clear a ListBox in powershell?

For clearing the listbox you are right in using listbox. Item. Clear() .

How do I clear a ListBox in Winforms?

BindingSource bindingSource = (BindingSource)listBox1. DataSource; IList SourceList = (IList)bindingSource. List; SourceList. Clear();


2 Answers

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

listBox1.Items.Clear(); 
like image 91
balexandre Avatar answered Oct 01 '22 14:10

balexandre


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;     } 
like image 37
Matt Dearing Avatar answered Oct 01 '22 14:10

Matt Dearing