简体   繁体   English

无法清除Winforms-ListBox

[英]Can't clear a Winforms-ListBox

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. 该列表框位于WinForms应用程序广告中,我想添加一个按钮来清除表单上的所有列表框。

It depends on which method you're using to fill the ListBox . 这取决于您使用哪种方法填充ListBox

  • If you're manually adding the items by calling listBox.Items.Add you can clear it through listBox.Items.Clear . 如果要通过调用listBox.Items.Add手动添加项目, listBox.Items.Add可以通过listBox.Items.Clear清除它。
  • If you're populating it through the DataSource, DisplayMember, and ValueMember properties you can set DataSource = null and then call listBox.Items.Clear in the DataSourceChanged event handler when the DataSource is null. 如果你通过DataSource,DisplayMember和ValueMember属性填充它,你可以设置DataSource = null ,然后调用listBox.Items.ClearDataSourceChanged事件处理程序的数据源为空。 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. 您看到的行为的另一种可能性是,一旦删除所有项目,列表框就会通过某些事件重新填充到您自己的代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM