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

时间:2017-02-02 09:17:30

标签: c# winforms combobox autocomplete focus

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

当组合框未聚焦时,我通过单击组合框旁边的箭头来聚焦它,项目列表会立即下降并折叠回来。随后点击箭头下拉列表因为组合框已经集中。

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

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

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

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

1 个答案:

答案 0 :(得分:3)

对于您的方案,只需通过BeginUpdate / EndUpdate来电进行ComboBox.Items修改即可:

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电话的即时反应,这是导致问题的原因。

相关问题