How can I control whether the text in a Textbox is automatically selected
(highlighted) upon entry into the Textbox? Sometimes when tabbing into a
Textbox, the text is automatically highlighted, and sometimes it is not. I
need to be able to control this. I want be able ot set it so that the text
is Never automatically highlighted upon entry into the Textbox.
VS-2005, Windows Forms
Thanks for any information.
-Gary
Eddie - 26 Sep 2008 15:12 GMT
> How can I control whether the text in a Textbox is automatically
> selected (highlighted) upon entry into the Textbox? Sometimes when
> tabbing into a Textbox, the text is automatically highlighted, and
> sometimes it is not.
IIRC, each control remembers its selection settings from the last time
it had focus.
> I need to be able to control this. I want be able ot set it so that
> the text is Never automatically highlighted upon entry into the
> Textbox.
Add a handler for the Enter or GotFocus event to set the SelectionStart
and SelectionLength properties.

Signature
Eddie eddie@deguello.org
kimiraikkonen - 01 Oct 2008 09:03 GMT
> How can I control whether the text in a Textbox is automatically selected
> (highlighted) upon entry into the Textbox? Sometimes when tabbing into a
[quoted text clipped - 7 lines]
>
> -Gary
Gary,
Try this:
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.Select(0, TextBox1.Text.Length)
End Sub
It must select(highlight) the entire text in textbox when it gets
focus. Plus, pressing TAB key between controls should be enough to
highlight text in textbox.
Hope this helps,
Onur Güzel
Jeff Johnson - 07 Oct 2008 21:43 GMT
>> How can I control whether the text in a Textbox is automatically selected
>> (highlighted) upon entry into the Textbox? Sometimes when tabbing into a
[quoted text clipped - 3 lines]
>> text
>> is Never automatically highlighted upon entry into the Textbox.
> Try this:
> Private Sub TextBox1_GotFocus(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles TextBox1.GotFocus
> TextBox1.Select(0, TextBox1.Text.Length)
> End Sub
Argh. And what happens when you click in the text box with the mouse?
Everything gets selected. This is NOT standard Windows behavior. If you go
this route you should employ some simple tracking of whether the mouse or
the keyboard was used to enter the text box.
Jack Jackson - 07 Oct 2008 23:25 GMT
>>> How can I control whether the text in a Textbox is automatically selected
>>> (highlighted) upon entry into the Textbox? Sometimes when tabbing into a
[quoted text clipped - 15 lines]
>this route you should employ some simple tracking of whether the mouse or
>the keyboard was used to enter the text box.
Microsoft recommends using the Enter event rather than GotFocus for
things like this, but I'm not sure it really matters.
In my experience, clicking in the control with code like above in the
Enter event does not select the text. The MouseClick event occurs
after the Enter event which then undoes the SelectAll() that I do in
Enter.
It isn't clear to me what the behavior should be when moving focus to
the textbox via a mouse click. I think it depends on what the user
intends to do to the textbox. Sometimes selecting all of the text is
probably what the user wants.
Jeff Johnson - 09 Oct 2008 14:44 GMT
> It isn't clear to me what the behavior should be when moving focus to
> the textbox via a mouse click. I think it depends on what the user
> intends to do to the textbox. Sometimes selecting all of the text is
> probably what the user wants.
Standard dialog box behavior is to select all text when tabbing, at least
for single-line text boxes. For multi-line text boxes I think it would be
user-friendly to remember the last position of the caret and restore that.
Or you could use the example that Access has put forth for a long time (at
least since I started working with it in 2.0): let the user choose whether
to select all, put the caret at the beginning, or at the end.