Textbox: Select Text On Tabed Focus

  • Thread starter Thread starter Phill
  • Start date Start date
P

Phill

I thought when you tabbed into a textbox that the text it contained
was automatically highlighted. This use to be the default in win32.

Is there an easy way to do this in .NET. I don't want this to happen
if the mouse is used to select the TextBox.

Seems wierd, If the Textbox has the default text in it the higlight
works but not if you edit it.

Thanks.
 
Hi Phill,

You can set textBox1.SelectAll() in the Enter event,
or if you use the same Enter event for all your textboxes

private void textbox_Enter(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}
 
calling SelectAll() on all your text boxes in the Page_Load should do the
trick

You'll only see the selection of the textbox that has focus, tabbing keeps
the selection, while a mouse click selects the mouse click, which, if is not
a drag, simply inserts the caret.
 
On the Enter event for the TextBox, you call SelectAll(). This will have the
effect you want. Clicking will place the mouse wherever you clicked without
selecting. You can program the event once and attach it to all TextBoxes on
your form.

private void textBox_Enter(object sender, System.EventArgs e)
{
txt = sender as TextBox;
if ( txt != null )
txt.SelectAll();
}

Or you can do a simple class than inherits from TextBox, and provide this
behavior if you need it in many places.

-Rachel
 
Back
Top