Armen Tsirunyan
Armen Tsirunyan

Reputation: 133092

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

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.

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:

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

Upvotes: 2

Views: 1708

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205769

For your scenario, it's simply a matter of enclosing the ComboBox.Items modification with BeginUpdate / EndUpdate calls:

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.

Upvotes: 3

Related Questions