【发布时间】:2011-01-28 11:30:53
【问题描述】:
我有一个组合框,我必须在其中显示数据库中的日期。用户必须从组合框中选择一个日期才能继续进行,但我不知道如何让用户知道首先从组合框中选择项目才能继续进行。
如果用户没有从组合中选择日期,应该遵循什么流程才能收到消息?
【问题讨论】:
标签: c# .net combobox selecteditem
我有一个组合框,我必须在其中显示数据库中的日期。用户必须从组合框中选择一个日期才能继续进行,但我不知道如何让用户知道首先从组合框中选择项目才能继续进行。
如果用户没有从组合中选择日期,应该遵循什么流程才能收到消息?
【问题讨论】:
标签: c# .net combobox selecteditem
if (string.IsNullOrEmpty(ComboBox.SelectedText))
{
MessageBox.Show("Select a date");
}
【讨论】:
这是检查组合框项目是否被选中的完美编码:
if (string.IsNullOrEmpty(comboBox1.Text))
{
MessageBox.Show("No Item is Selected");
}
else
{
MessageBox.Show("Item Selected is:" + comboBox1.Text);
}
【讨论】:
你可以用这个:
if (Convert.ToInt32(comboBox1.SelectedIndex) != -1)
{
// checked
}
else
{
// unckecked
}
【讨论】:
您需要使用 DropDownStyle = DropDownList,这样您就可以轻松确保用户从列表中选择了一个条目,并且不能在框中输入随机文本。在填充之前向项目添加一个空项目(或“请选择”)。现在,默认自动为空,测试很简单:只需检查 SelectedIndex > 0。
【讨论】:
像这样检查文本属性
if (combobox.text != String.Empty)
{
//continue
}
else
{
// error message
}
【讨论】:
if (cboDate.SelectedValue!=null)
{
//there is a selected value in the combobox
}
else
{
//no selected value
}
【讨论】:
if(combobox.Selectedindex==-1)
{
MessageBox.Show("Please Select an item");
}
else
{
MessageBox.Show("An Item was selected");
}
【讨论】:
您可以使用ComboBox 的SelectedIndex 或SelectedItem 属性。
【讨论】:
Pl。注意 ComboBox.Text 仅检查位于 ComboBox 可编辑区域的 Text,因此当您要检查 ComboBox 内是否有某些选择时,不应该使用它。
这将永远有效。
int a = ComboBox.SelectedIndex.CompareTo(-1);
if (a == 0)
{
MessageBox.Show("Please select something.");
}
else
{
// do something if combo box selection is done.!
}
【讨论】: