如何强制用户采取建议进入一个ComboBox? [英] How to force a user to take a suggested entry into a ComboBox?

查看:143
本文介绍了如何强制用户采取建议进入一个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 来包含它自己的数据源的值

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

示例:参赛作品必须建议用户...如果我写的CO,组合应建议刚果和哥伦比亚,但只有那些值中的一个应该是由用户输入。用户不应该引入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.

谢谢!

推荐答案

您必须使用combobox.autocompletemode得到l​​iek刚果,会议的名称,当用户输入如CO的文字

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);

然后填充COMBOX项目源状物体,重写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;
  }
}

编辑:您需要做验证由在组合框中用户输入的文本,如果用户选择下拉项目通过自动completemode提出的验证返回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 ...

做类似下面...

创建一个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.

如果输入是pressed调用验证函数,会做任何你觉得必要的,如果输入的值等于与名字被存储在数据库中...

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...

可以离开组合框,直到一个有效的选择是调用的离开事件处理同样的功能,以prevent用户。

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天全站免登陆