如何强制用户将建议条目输入组合框?

发布于 2024-12-09 20:53:32 字数 278 浏览 0 评论 0原文

我希望用户从组合框中选择一个值。 必须在用户的文本输入中建议条目。

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

示例:必须向用户建议条目...如果我写“CO”,组合应建议“CONGO”和“COLOMBIA”,但用户只应输入其中一个值用户。用户不应引入“COfdfgdfg”或任何随机字符串。

谢谢!

I want a user to select a value from a 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?

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.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2024-12-16 20:53:32

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

Set the combo box's style to ComboBoxStyle.DropDownList

回眸一遍 2024-12-16 20:53:32

您必须使用combobox.autocompletemode来获取名称liek Congo,congress当用户输入“CO”之类的文本时,

您可以将自己的数据源添加到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方法的对象填充组合框项目源

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

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

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

执行如下操作。 ..

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

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

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

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

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

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

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

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

Do something like below ...

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

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();
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文