C# 清除组合框选定的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9999458/
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
Clear ComboBox selected text
提问by phadaphunk
I have a ComboBoxcontrol with the DropDownStyleproperties set to DropDownList.
Once there is an item selected, how can I clear the selection from the ComboBoxwithout deleting any Items in it ?
我有一个ComboBox与控制DropDownStyle设置的属性DropDownList。一旦选择了一个项目,我如何清除选择ComboBox而不删除其中的任何项目?
I'd normally use something like that:
我通常会使用类似的东西:
myComboBox.Text.Clear();
But I can't do that. Any idea how I could clear it ?
但我不能那样做。知道如何清除它吗?
采纳答案by ionden
You could change SelectedIndexproperty:
您可以更改SelectedIndex属性:
comboBox1.SelectedIndex = -1;
回答by Ami Schreiber
Try specifying the actual index of the item you want erase the text from and set it's Text equal to "".
尝试指定要从中删除文本的项目的实际索引,并将其文本设置为等于“”。
myComboBox[this.SelectedIndex].Text = ""
myComboBox[this.SelectedIndex].Text = ""
or
或者
myComboBox.selectedIndex.Text = ""
myComboBox.selectedIndex.Text = ""
I don't remember the exact syntax but it's something along those lines.
我不记得确切的语法,但它是沿着这些路线的。
回答by Darrell Lloyd Harvey
The only way I could get it to work:
我可以让它工作的唯一方法:
comboBox1.Text = "";
For some reason ionden's solution didn't work for me.
出于某种原因,ionden 的解决方案对我不起作用。
回答by user4798349
comboBox1.Text = " ";
comboBox1.Text = " ";
This is the best and easiest way to set your combo box back to default settings without erasing the contents of the combo box.
这是在不删除组合框内容的情况下将组合框设置回默认设置的最佳和最简单的方法。
回答by Hussein Qadri
write the following code:
编写以下代码:
comboBox1.Items[comboBox1.SelectedIndex] = string.Empty;
回答by Mario Cocozza
all depend on the configuration. for me works
一切都取决于配置。对我有用
comboBox.SelectedIndex = -1;
my configuration
我的配置
DropDownStyle: DropDownList
(text can't be changed for the user)
(不能为用户更改文本)
回答by Naqeeb Ahmed
nameofcombobox.SelectedItem=-1;
回答by Justin Dean
The following code will work:
以下代码将起作用:
ComboBox1.SelectedIndex.Equals(String.Empty);
回答by user11040190
In c# if you make you comboBox configuration style DropDownListor DropDownthen use both of them in this method to clear.
在c#中,如果你让你的comboBox配置样式DropDownList或者DropDown在这个方法中同时使用它们来清除。
ComboBox1.SelectedIndex = -1;
回答by Ya?mur Kopar
This is what you need:
这是你需要的:
comboBox1.ResetText();
comboBox1.ResetText();

