【问题标题】:Check if the value currently in combobox is contained in items of that combo检查当前组合框中的值是否包含在该组合的项目中
【发布时间】:2013-04-16 19:00:47
【问题描述】:

我有 Control - ComboBox,它是语言列表。我通过右键单击该组合-> 属性-> 选择那里的属性,然后-> 包含的项目(...),在该组合中添加了语言。我已经逐行添加了几种语言。

问题是用户可以通过编写该组合框中的内容来手动更改,而不仅仅是他可以通过那里选择的内容。因此,我需要查看当前在 comboBox 中写入的内容是否包含在 Items 中。我已将此功能添加到事件“验证”中:

private bool ValidatingComboBox(ComboBox sender)
{
   if (sender.SelectedValue != null)
   {
      if (sender.Items.Contains(sender.SelectedValue))
      {
         errorProvider.SetError(sender, "");
         return true;
      }

      else
      {
         errorProvider.SetError(sender, "No such thing in list!");
         return false;
      }
   }

   else
   {
      errorProvider.SetError(sender, "No such thing in list!");
      return false;
   }
}

但是由于某种原因,当我在组合框中写“英语”时,它仍然会给我一个错误,尽管英语是在项目中。不过,当我通过在下拉列表中用鼠标选择语言来更改语言时,它不会报告错误。

我做错了什么?

【问题讨论】:

    标签: c# combobox validating


    【解决方案1】:

    您可以首先阻止用户在那里输入...如果他不能输入,则不需要此验证..试试这个

    this.comboBoxType.DropDownStyle = ComboBoxStyle.DropDownList;
    

    【讨论】:

    • 很公平。我没有想到应该有办法在里面禁用手动写入。谢谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2022-08-18
    相关资源
    最近更新 更多