4
votes

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?

2
see comments to Kyle Rozendo.BuddyJoe

2 Answers

2
votes

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

    }
}
0
votes

Well, you won't need to use Focus() if you use Select(0,0), so I don't see the problem? It still ends up as a single call.