简体   繁体   English

如何强制用户将建议的条目带入ComboBox?

[英]How to force a user to take a suggested entry into a ComboBox?

I want a user to select a value from a ComboBox. 我希望用户从ComboBox中选择一个值。 Entries have to be suggested at user's text input. 必须在用户的文本输入中建议条目。

Do I have to use events to enforce a System.Windows.Forms.ComboBox to contain a value from its own DataSource ? 我是否必须使用事件来强制System.Windows.Forms.ComboBox包含来自其自己的DataSource的值?

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. 示例:必须向用户建议条目...如果我写“CO”,组合应该建议“CONGO”和“COLOMBIA”,但用户只能输入其中一个值。 The user should not introduce "COfdfgdfg" or any random string. 用户不应该引入“COfdfgdfg”或任何随机字符串。

Thanks! 谢谢!

将组合框的样式设置为ComboBoxStyle.DropDownList

You have to use combobox.autocompletemode to get the names liek Congo,congress when the user enter the text like "CO" 您必须使用combobox.autocompletemode来获取名称liek Congo,当用户输入“CO”这样的文本时会议

you can your own datasource to comboBox1.AutoCompleteSource 你可以将自己的数据源改为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);

Then populate the combox items source with objects like, that override the ToString method 然后使用覆盖ToString方法的对象填充combox项源

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

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

Edit: 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 ... 编辑:您必须对用户在组合框中输入的文本进行验证,如果用户选择auto completemode建议的下拉项,则验证返回true,否则返回false ...

Do something like below ... 做下面的事......

Create a KeyDown event handler for the combobox and check for an Enter key. 为组合框创建一个KeyDown事件处理程序并检查Enter键。 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... 如果输入被按下,则调用一个验证函数,如果输入的值与存储在数据库中的名称相等,它将执行您认为必要的任何操作...

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. 您可以在Leave事件处理程序中调用此相同的函数,以防止用户在进行有效选择之前离开组合框。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM