【问题标题】:Update ComboBox Items in Enter event handler with Autocomplete使用自动完成更新 Enter 事件处理程序中的 ComboBox 项
【发布时间】:2017-06-19 07:02:13
【问题描述】:

我有一个组合框。它的自动完成模式设置为“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);
    }
}

【问题讨论】:

  • 你能试试if (sender.GetType() != typeof(System.Windows.Controls.ComboBox)) { //do stuff only if sender is the one you want }这样的活动吗
  • @Madenis:我不确定我是否理解。你能详细说明一下吗?
  • 或者现在我想起来了,也许事件会触发,清除列表并在每次输入时重新填充它。因此,最好将列表填充逻辑移至其他事件,例如 comboBox1_loaded
  • @Madenis:这是不可行的,因为列表取决于其他输入中选择的值。
  • 是否可以在初始化块上执行comboBox1.Items = BindingList&lt;T&gt;() 并在程序运行时自动填充它,因此您根本不需要 Enter 事件?

标签: c# winforms combobox autocomplete focus


【解决方案1】:

对于您的方案,只需将 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 呼叫立即做出反应。

【讨论】:

  • 谢谢你,看来你是正确的
  • 如果是你要找的,请标记为正确答案。
猜你喜欢
  • 1970-01-01
  • 2012-12-20
  • 2014-12-16
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
相关资源
最近更新 更多