C#中如何检查组合框中的项目是否被选中?
我有一个组合框,必须在其中显示数据库中的日期。用户必须从组合框中选择一个日期才能进一步进行,但我不知道如何让用户意识到首先从组合框中选择该项目才能进一步进行。
如果用户没有从组合中选择日期,应该遵循什么流程才能收到消息?
I have a combo box in which I have to display the dates from a database. The user has to select a date from the combo box to proceed further, but I don't know how to make the user aware of selecting the item from the combo box first in order to proceed further.
What process should be followed so that a user can get a message if he has not selected the date from the combo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是检查组合框项目是否被选中的完美编码:
Here is the perfect coding which checks whether the Combo Box Item is Selected or not:
你可以使用这个:
You can use this:
您将需要使用 DropDownStyle = DropDownList,以便您可以轻松确保用户从列表中选择了一个条目,并且无法在框中键入随机文本。在填充之前将空项目添加到项目中(或“请选择”)。现在,默认值自动为空,测试也很简单:只需检查 SelectedIndex > 即可。 0。
You'll want to use DropDownStyle = DropDownList so you can easily make sure that the user picked an entry from the list and can't type random text in the box. Add an empty item to Items before you populate it (or "Please select"). Now, the default is automatically empty and the test is simple: just check that SelectedIndex > 0.
像这样检查文本属性
check the text property like this
您可以使用
SelectedIndex
或 SelectedItem 属性。You can use
SelectedIndex
or SelectedItem properties of theComboBox
.PL。注意 ComboBox.Text 仅检查位于 ComboBox 可编辑区域的文本,因此当您想要检查 ComboBox 中是否存在某些选择时,不应使用它。
这永远有效。
Pl. note ComboBox.Text only checks for the Text that is at the editable region of the ComboBox, so that's not supposed to be used when you want to check if there's some selection from within the ComboBox.
This will work always.