检查组合框值是否为空 [英] Check if combobox value is empty

查看:62
本文介绍了检查组合框值是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用三个值创建了一个ComboBox。我希望没有选择任何项目时打开一个消息框,所以我尝试了此操作:

I have created a ComboBox with three values. I wanted that a message box opens when no item is selected so I tried this:

if (comboBox1.SelectedItem == null)
{
    MessageBox.Show("Please select a value");
    return;
}

这很好,但只有在我单击组合框内的字段时才可以。当我不触摸它时,程序将启动而没有消息框。

That works fine but only if I click into the field in the combobox. When I dont touch it, the program will start without message box. Whats wrong?

推荐答案

if(string.IsNullOrEmpty(comboBox1.Text)) if(comboBox1.SelectedIndex == -1)

这篇关于检查组合框值是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆