C# 如何检测是否未选择 ComboBox 上的选定项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12876056/
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
How do I detect if no selected item on ComboBox is chosen?
提问by Hendra Anggrian
In my ComboBox, the field is blank before users click it and choose any item. So without users click on the ComboBox, it remains empty. How do we check whether the ComboBox is empty or not?
在我的 ComboBox 中,该字段在用户单击它并选择任何项目之前是空白的。因此,如果没有用户点击 ComboBox,它仍然是空的。我们如何检查 ComboBox 是否为空?
This codes gives me an error because there is no item selected yet:
此代码给我一个错误,因为尚未选择任何项目:
if( ComboBox.SelectedItem.ToString().Equals("") )
{
//do something
}
采纳答案by Jcl
if( ComboBox.SelectedItem == null ) {
// do something
}
回答by inzenir
ComboBox.SelectedItems.Count
this should work :P it counts selected items. if that number is 0, no items are selected.
这应该有效:P 它计算选定的项目。如果该数字为 0,则不选择任何项目。
回答by Geiziry
if( ComboBox.SelectedIndex == -1 )

