[英]Autocomplete on Combobox onkeypress event eats up the Enter key
[英]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
調用立即做出反應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.