我有一个组合框。其自动完成模式设置为“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
调用立即使用react。
关于c# - 使用自动完成功能更新 Enter 事件处理程序中的组合框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41998299/