Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / October 2008

Tip: Looking for answers? Try searching our database.

TextBox text selected on entry

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GL-DSHS - 26 Sep 2008 14:30 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.  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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.