R – .NET – WinForm Textboxes – Focus and SelectedText

nettextboxwinforms

Whenever I set focus to a Textbox in WinForms (.NET 3.5) the entire text is selected. Does not matter if I have MultiLine set to true or false. Seems to be the exact reverse of what this user is seeing:
Making a WinForms TextBox behave like your browser's address bar

I have tried to do:

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

Is there another property I can set to stop this annoying behavior?

I just noticed this works:

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

But do I really need to call select() on all my textboxes?

Best Answer

Create a custom TextBox control that overrides the Enter event.

Something like this:

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);
        }

    }
}