.NET - WinForm文本框 - Focus和SelectedText

时间:2009-09-08 18:11:11

标签: .net winforms textbox

每当我将焦点设置为WinForms(.NET 3.5)中的文本框时,都会选择整个文本。如果我将MultiLine设置为true或false,则无关紧要。似乎与该用户所看到的完全相反: Making a WinForms TextBox behave like your browser's address bar

我试过这样做:

    private void Editor_Load(object sender, EventArgs e)
    {
       //form load event
       txtName.SelectedText = String.Empty; // has no effect
    }

我可以设置另一个属性来阻止这种恼人的行为吗?

我刚注意到这件事有效:

        txtName.Select(0,0);
        txtScript.Select(0,0);

但我真的需要在我的所有文本框上调用select()吗?

2 个答案:

答案 0 :(得分:2)

创建一个覆盖Enter事件的自定义TextBox控件。

这样的事情:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace YourNamespace  
{
    class MyTextBox : TextBox
    {

        protected override void OnEnter(EventArgs e) {
            this.Select(0, 0);

            base.OnEnter(e);
        }

    }
}

答案 1 :(得分:0)

嗯,如果您使用Focus(),则无需使用Select(0,0),所以我没有看到问题?它仍然只是一个电话。