0
在C#中的Windows.Forms表單中,我有一個組合框和一個文本框。該文本框通過AutoCompleteCustomSource打開了自動完成功能。 AutoCompleteCustomSource中的元素取決於組合框的選定值。這意味着每次更改組合框的值時都必須更改自動完成值。但這樣做我也經歷過一些奇怪的行爲,我不喜歡:在C#中使用奇怪行爲自動完成文本框
- 文本是自動完成的離開並重新輸入文本框
- 離開並重新輸入文本框時後一直選擇,光標總是會放置在文本的末尾,即使我點擊使用退格鍵的字符
- 之間的某處使suggestbox出現
我有以下簡短的示例代碼示出了所描述的行爲。 嘗試在文本框中輸入「必須」。按下鍵盤上的TAB後,應該建議並附加「Mustang」。現在從組合框中選擇「福特」,然後重新輸入文本框來查看我的意思。
Form2.Designer.cs
partial class Form2
{
/// <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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Items.AddRange(new object[] {
"Audi",
"Fiat",
"Ford",
"VW"});
this.comboBox1.Location = new System.Drawing.Point(87, 12);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(193, 21);
this.comboBox1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(1, 15);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(38, 13);
this.label1.TabIndex = 1;
this.label1.Text = "Brand:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(1, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(34, 13);
this.label2.TabIndex = 2;
this.label2.Text = "Type:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(87, 61);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(193, 20);
this.textBox1.TabIndex = 3;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(12, 110);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(268, 144);
this.textBox2.TabIndex = 4;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
}
Form2.cs
public partial class Form2 : Form
{
private string[] types_audi = new string[] { "A4" };
private string[] types_ford = new string[] { "Mustang", "Focus" };
private string[] types_fiat = new string[] { "Punto", "500" };
private string[] types_vw = new string[] { "Golf" };
private List<string[]> types = new List<string[]>();
public Form2()
{
InitializeComponent();
this.types.Add(this.types_audi);
this.types.Add(this.types_ford);
this.types.Add(this.types_fiat);
this.types.Add(this.types_vw);
this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.textBox1.AutoCompleteCustomSource = new AutoCompleteStringCollection();
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_audi);
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_fiat);
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_ford);
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_vw);
this.textBox1.Enter += new EventHandler(this.textBox1_Enter);
this.textBox1.Leave += new EventHandler(this.textBox1_Leave);
this.comboBox1.Select();
}
private void textBox1_Leave(object sender, EventArgs e)
{
this.textBox2.Clear();
}
private void textBox1_Enter(object sender, EventArgs e)
{
// reset AutoCompleteCustomSource
this.textBox1.AutoCompleteCustomSource.Clear();
switch (this.comboBox1.SelectedItem as string)
{
case "Audi":
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_audi);
break;
case "Ford":
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_ford);
break;
case "Fiat":
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_fiat);
break;
case "VW":
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_vw);
break;
default:
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_audi);
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_fiat);
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_ford);
this.textBox1.AutoCompleteCustomSource.AddRange(this.types_vw);
break;
}
this.textBox2.Text = "Possible values: " + Environment.NewLine;
foreach (var val in this.textBox1.AutoCompleteCustomSource)
{
this.textBox2.Text += Environment.NewLine + val;
}
}
}
你有autocompletemode設置建議和AutoCompleteSource設置時listItems – MethodMan 2012-08-13 14:36:43