如果未选择组合框,则异常 [英] exception if combobox is not selected
本文介绍了如果未选择组合框,则异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好.我想知道,如果未选择组合框,该如何做例外.到目前为止,它会出现错误:对象引用未设置为对象的实例.如果可能的话,我希望它像请从列表中选择一个项目"那样出现. 或类似的东西.
Hello. I want to know, how do I make an exception if the combobox is not selected. So far, it will come out Error: Object reference not set to an instance of an objet. If possible, I want it to come out like "Please select an item from the list." or something like that.
string LabName = comboBoxLabNumber.SelectedItem.ToString();
string LabNumber = LabName;
string LabProblem = comboBoxProblem.SelectedItem.ToString();
string LabProb = LabProblem;
还是我应该使用if/else来做?有什么主意吗?
谢谢
Or should I just do it using if/else? Any idea?
Thanks
推荐答案
将SelectedIndex用于组合框.
Hi,
using SelectedIndex for combobox.
if(comboBoxLabNumber.SelectedIndex > -1)
{
LabName = comboBoxLabNumber.SelectedItem.ToString();
}
else
{
MessageBox.Show("Please select an item!");
}
尝试这种方式...
干杯:)
Try this way...
Cheers :)
然后处理异常,例如
Then handle the exception, for instance
try
{
LabName = comboBoxLabNumber.SelectedItem.ToString();
}
catch (Exception e)
{
MessageBox.Show("Please select an item from the list");
// here take the appropriate action.
}
这篇关于如果未选择组合框,则异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文