简体   繁体   English

使用自动完成功能更新Enter事件处理程序中的ComboBox项目

[英]Update ComboBox Items in Enter event handler with Autocomplete

I have a ComboBox. 我有一个组合框。 Its autocomplete mode is set to "SuggestAppend" and the autocomlete source is set to "ListItems". 其自动完成模式设置为“ SuggestAppend”,自动完成源设置为“ ListItems”。 If I try to update the list of items of that combo box in the combo box's Enter event handler, I get the following unexpected behavior. 如果我尝试更新组合框的Enter事件处理程序中该组合框的项目列表,则会出现以下意外行为。

When the combobox is NOT focused and I focus it by clicking the arrow next to the combo box, the list of items drops and collapes back instantly. 当组合框未聚焦时,我通过单击组合框旁边的箭头将其聚焦,则项目列表将掉落并立即折叠。 Subsequent click on the arrow drop down the list fine because the combo box is already focused. 由于组合框已经聚焦,因此随后在箭头上单击下拉列表会很好。

I suspect that's because when I update the item list inside the Enter event handler, another event is fired (item list changed ?) for the autocomplete to handle its magic, which triggers the combo box to collapse. 我怀疑这是因为,当我在Enter事件处理程序中更新项目列表时,会触发另一个事件(项目列表已更改?)以使自动完成功能处理其魔力,从而触发组合框折叠。

What can I do about this? 我该怎么办? Do note that it is somewhat important for me to update the list of items in the combobox only when I know that that combobox is going to be used soon (hence the enter event handler). 请注意,仅当我知道该组合框将要很快使用时,才对组合框中的项目列表进行更新对我来说很重要(因此,输入事件处理程序)。

Here's a minimal compilable example (the button is there so that the combobox isn't initially focused): 这是一个最小的可编译示例(按钮在那里,因此组合框最初并未处于焦点状态):

Form1 Designer: Form1设计器:

partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // comboBox1
        // 
        this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
        this.comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
        this.comboBox1.FormattingEnabled = true;
        this.comboBox1.Location = new System.Drawing.Point(50, 83);
        this.comboBox1.Name = "comboBox1";
        this.comboBox1.Size = new System.Drawing.Size(190, 24);
        this.comboBox1.TabIndex = 1;
        this.comboBox1.Enter += new System.EventHandler(this.comboBox1_Enter);
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(165, 35);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(282, 255);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.comboBox1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
    }

    #endregion

    private System.Windows.Forms.ComboBox comboBox1;
    private System.Windows.Forms.Button button1;
}

Form1.cs: Form1.cs中:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void comboBox1_Enter(object sender, EventArgs e)
    {
        string[] items = new string[] { "A", "B", "C" };          
        comboBox1.Items.Clear();
        comboBox1.Items.AddRange(items);
    }
}

For your scenario, it's simply a matter of enclosing the ComboBox.Items modification with BeginUpdate / EndUpdate calls: 对于您的方案,只需将ComboBox.Items修改包含在BeginUpdate / EndUpdate调用中即可:

private void comboBox1_Enter(object sender, EventArgs e)
{
    string[] items = new string[] { "A", "B", "C" };
    comboBox1.BeginUpdate();
    comboBox1.Items.Clear();
    comboBox1.Items.AddRange(items);
    comboBox1.EndUpdate();
}

It prevents immediate reacting on Items.Clear call which was the cause of the issue. 它可以防止对引起问题的Items.Clear调用立即做出反应。

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

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