C# 如何清除组合框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9321844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:53:55  来源:igfitidea点击:

How do I clear a combobox?

c#winformscombobox

提问by Fuzz Evans

I have some combo-boxes that are set up as drop down lists, and the user can pick a number in them. I also have a Clear button that should clear the text from the combo boxes but I can't seem to get it. I've tried:

我有一些设置为下拉列表的组合框,用户可以在其中选择一个数字。我还有一个清除按钮,应该清除组合框中的文本,但我似乎无法得到它。我试过了:

 //doesn't work
 cboxHour.Text = "";

and

//doesn't work
cboxHour.ResetText();

This seems like it should be so straight forward but I'm just not getting it.

这似乎应该如此简单,但我只是不明白。

采纳答案by Daniel Mann

Did you try cboxHour.Items.Clear()?

你试了cboxHour.Items.Clear()吗?

回答by Hand-E-Food

If you just want to clear the current selection, but leave all of the items in the list, you can use:

如果您只想清除当前选择,但保留列表中的所有项目,您可以使用:

cboHour.SelectedIndex = -1

回答by user2415339

cboxHour.Items.Clear();

this works

这有效

回答by Zen

You can use

您可以使用

Cbo.Items.Clear();

or

或者

Cbo.DataSource = null;

if you have a binding on it.

如果你有绑定。

回答by Tapan kumar

Combo Box, DropDown all are having the same logic to clear/remove all items from them and it is like below.

Combo Box、DropDown 都具有相同的逻辑来清除/删除它们中的所有项目,如下所示。

//For checkbox list
cblTest.Items.Clear();

//For drop down list
ddlTest.Items.Clear();

回答by beanmf

When ComboBoxis not data-bound, I've found I need both: Clear()removes the items but still leaves the SelectedItem's text, while ResetText()removes that text. VS2008.

ComboBox不是数据绑定时,我发现我需要两者: Clear()删除项目但仍保留SelectedItem's 文本,同时ResetText()删除该文本。 VS2008

ComboBox.Items.Clear();
ComboBox.ResetText();

回答by Rohil Patel

If you have applied datasource to combobox, then it will not be cleared as cmb.Items.Clear().

如果您已将数据源应用于组合框,则它不会被清除为cmb.Items.Clear().

For that you have to assign datasource nullto combobox.

为此,您必须将数据源分配null给组合框。

cmb.DataSource = null;
cmb.Items.Clear();

回答by user5331024

Mine worked with:

我曾与:

ComboBox.removeAllItems();

If it doesn't read that well its, remove all items.

如果它读得不好,请删除所有项目。

回答by user5458887

private void Resetbtn_Click(object sender, EventArgs e)
{    
    comboBox1.Items.Clear(); // it will clear a combobox

    comboBox1.Items.Add("Student"); //then add combobox elements again. 
    comboBox1.Items.Add("Staff");
}

回答by user5589898

Answer for your question is:

你的问题的答案是:

metroComboBox1.SelectedItem = null;
anycomboBox1.SelectedItem=null;