如何强制用户将建议的条目输入 ComboBox? [英] How to force a user to take a suggested entry into a ComboBox?

查看:14
本文介绍了如何强制用户将建议的条目输入 ComboBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户从 ComboBox 中选择一个值.条目必须在用户的文本输入处被建议.

I want a user to select a value from a ComboBox. Entries have to be suggested at user's text input.

我是否必须使用事件来强制 System.Windows.Forms.ComboBox 包含来自它自己的 DataSource 的值?

Do I have to use events to enforce a System.Windows.Forms.ComboBox to contain a value from its own DataSource?

示例: 必须向用户建议条目...如果我写CO",组合应该建议CONGO"和COLOMBIA",但只有这些值之一应该是由用户输入.用户不应引入COfdfgdfg"或任何随机字符串.

Example: Entries have to be suggested to the user... If I write "CO", the combo should suggest "CONGO" and "COLOMBIA", but only one of those values should be entered by the user. The user should not introduce "COfdfgdfg" or any random string.

谢谢!

推荐答案

当用户输入诸如CO"之类的文本时,您必须使用combobox.autocompletemode 来获取诸如Congo、congress 之类的名称

You have to use combobox.autocompletemode to get the names liek Congo,congress when the user enter the text like "CO"

你可以自己的数据源到comboBox1.AutoCompleteSource

you can your own datasource to comboBox1.AutoCompleteSource

 this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(113, 192);
 this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);

然后使用覆盖 ToString 方法的对象填充组合框项目源

Then populate the combox items source with objects like, that override the ToString method

public class POCLRO
{
  public int ID { get; set; }
  public string Name { get; set; }

  public override string ToString()
  {
    return Name;
  }
}

您必须对用户在组合框中输入的文本进行验证,如果用户选择自动完成模式建议的下拉项,则验证返回 true 否则返回 false ...

You have to do validation on text entered by the user in combobox , if the user selects the drop down item suggested by auto completemode the validation returns true else it returns false ...

做类似下面的事情......

Do something like below ...

为组合框创建一个 KeyDown 事件处理程序并检查 Enter 键.请注意,在用户点击后,组合框中的文本将被选中(就像在执行剪切或复制操作一样被选中),并且焦点仍保留在组合框中.

Create a KeyDown event handler for the combobox and check for an Enter key. Note that after the user hits enter the text in the combobox is selected (as in, selected as if you were doing a cut or copy operation) and focus remains in the combobox.

如果按下回车键,则调用验证函数,如果输入的值与存储在数据库中的名称相同,则该函数将执行您认为必要的任何操作...

If enter was pressed call a validation function that will do whatever you feel necessary if the value entered is equal with name that was stored in database...

您可以在 Leave 事件处理程序中调用相同的函数,以防止用户在做出有效选择之前离开组合框.

You can call this same function in a Leave event handler to prevent the user from leaving the combobox until a valid selection is made.

 private void ComboBox_KeyDown(object sender, KeyEventArgs e)
 {
    if (e.KeyCode == Keys.Enter)
    {
        ValidateSelection();
    }
 }

 private  bool validation()
 {
   // do validation here 
 }
private void ComboBox_Leave(object sender, EventArgs e)
{
    if(!ValidateSelection())
    {
        ComboBox.Focus();
    }
 }

这篇关于如何强制用户将建议的条目输入 ComboBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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