BuddyJoe
BuddyJoe

Reputation: 71101

.NET - WinForm Textboxes - Focus and SelectedText

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?

Upvotes: 4

Views: 2311

Answers (2)

Jay Riggs
Jay Riggs

Reputation: 53593

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

    }
}

Upvotes: 2

Kyle Rosendo
Kyle Rosendo

Reputation: 25277

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.

Upvotes: 0

Related Questions