This is a rather simply question - but it's defeated me so far.
I have two list boxes on my form. When the user clicks an item in the first box, the second list box populates with related values. However sometimes I later want to clear the second listbox so it appears empty again.
However, none of the following seems to work:
lbxQuantity.Items.Clear();
lbxQuantity.DataSource = null;
lbxQuantity.Text = "";
How do I completely clear the listbox of all values and just leave it blank?
The listbox is in a WinForms application ad I'd like to add a button that clears all listboxes on the form.
It depends on which method you're using to fill the ListBox.
listBox.Items.Add you can clear it through listBox.Items.Clear.DataSource = null and then call listBox.Items.Clear in the DataSourceChanged event handler when the DataSource is null. You can of course also do it right after the null assignment.Another possibility for the behavior you're seeing could be that listbox is repopulated in your own code through some event as soon as you remove all items.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With