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

Update ComboBox Items in Enter event handler with Autocomplete

我有一个组合框。它的自动完成模式设置为 "SuggestAppend",自动完成源设置为 "ListItems"。如果我尝试在组合框的 Enter 事件处理程序中更新该组合框的项目列表,则会出现以下意外行为。

当组合框未获得焦点并且我通过单击组合框旁边的箭头将其聚焦时,项目列表立即下降并折叠回去。随后单击箭头下拉列表很好,因为组合框已经聚焦。

我怀疑这是因为当我在 Enter 事件处理程序中更新项目列表时,会触发另一个事件(项目列表已更改?),以便自动完成处理它的魔法,这会触发组合框折叠。

我该怎么办?请注意,仅当我知道该组合框将很快被使用(因此输入事件处理程序)时,才更新组合框中的项目列表对我来说有点重要。

这是一个最小的可编译示例(按钮在那里,因此组合框最初没有聚焦):

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:

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

对于您的场景,只需将 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();
}

它阻止了对导致问题的 Items.Clear 调用的立即反应。