如何检查是否从 C# 中的组合框中选择了项目 [英] how to check if item is selected from a comboBox in C#

查看:12
本文介绍了如何检查是否从 C# 中的组合框中选择了项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.

我有一个表格,想检查用户是否正确填写.在表单中有一个组合框;我如何构建if"语句来检查用户是否从中选择了一个项目?

I have a form, and want to check if the user filled it in correctly. In the form there's a combo box; how can I build the "if" statement for checking whether the user picked an item from it ?

附言抱歉我的英语不好,这不是我的母语.:)

P.S. Sorry for my bad English, it's not my mother tongue. :)

推荐答案

使用:

if(comboBox.SelectedIndex > -1) //somthing was selected

要获取所选项目,您可以:

To get the selected item you do:

Item m = comboBox.Items[comboBox.SelectedIndex];

正如马修正确指出的,要获得所选项目,您也可以这样做

As Matthew correctly states, to get the selected item you could also do

Item m = comboBox.SelectedItem;

这篇关于如何检查是否从 C# 中的组合框中选择了项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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