Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 检查组合框项目中是否有用户输入_C#_Winforms_Combobox_Validating - Fatal编程技术网

C# 检查组合框项目中是否有用户输入

C# 检查组合框项目中是否有用户输入,c#,winforms,combobox,validating,C#,Winforms,Combobox,Validating,这就是我如何检查用户是否在组合框中输入空 if (string.IsNullOrEmpty(comboBox.Text)) { MessageBox.Show("No Item is Selected"); } 如何检查用户输入是否在组合框项目中?例如,组合框项目是a、b、c。当用户在组合框中输入“d”然后离开时,必须显示一个消息框。您可以尝试像George所说的那样在组合框的Leave EventHandler中放入类似的内容,检查该项是否包含在组合框的项集合中 private void

这就是我如何检查用户是否在组合框中输入空

if (string.IsNullOrEmpty(comboBox.Text))
{
MessageBox.Show("No Item is Selected"); 
}

如何检查用户输入是否在组合框项目中?例如,组合框项目是a、b、c。当用户在组合框中输入“d”然后离开时,必须显示一个消息框。

您可以尝试像George所说的那样在组合框的Leave EventHandler中放入类似的内容,检查该项是否包含在组合框的项集合中

private void comboBox1_Leave(object sender, EventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    if (! cb.Items.Contains(cb.Text))
    {
        MessageBox.Show("No Item is Selected");
    }
}
试试这个:

int resultIndex = -1;
resultIndex = comboBox.FindExactString("d");

if(resultIndex == -1)
{
    MessageBox.Show("No Item is Selected");
}

在这种情况下,@Mark Hall给出的答案是正确的。但是如果您想限制用户不使用组合框中项目集合之外的项目,我建议您将DropDownStyle属性改为DropDownList

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

“组合框中的用户输入d”是什么意思?为组合框中的每个元素添加lostfocus事件