选择文本框中的所有文本并自动完成(c# winforms)

本文关键字:文本 winforms 选择 | 更新日期: 2023-09-27 17:50:49

我创建了一个具有自动完成功能的文本框,但是我遇到了以下问题。当我按Ctrl+A选择文本框中的所有文本时,文本就会消失。

这是我的文本框的源代码:

        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox1.AutoCompleteCustomSource.AddRange(new string[] {
        "hello",
        "test",
        "ahha",
        "haha"});
        this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
        this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
        this.textBox1.Location = new System.Drawing.Point(13, 13);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 20);
        this.textBox1.TabIndex = 0;

我希望文本突出显示而不是消失。

选择文本框中的所有文本并自动完成(c# winforms)

如果我添加以下代码,行为停止:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == (Keys.Control | Keys.A))
    {
        SelectAll();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

但我仍然不确定为什么自动完成模式上的附加功能删除文本而不重写Ctrl+A

如果你做这个改变,它似乎工作:

this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;