Connecting Tech Pros Worldwide Help | Site Map

VB Highlight all text in textbox by clicking

Newbie
 
Join Date: Jun 2006
Posts: 3
#1: Jun 12 '06
sorry if this is not the right place to post this

I was wondering how I could highlight everything in a textbox just by clicking on it.

example: textbox1 has "0" as text, I want to click(it automatically highlights/selects all) and type 1, instead of having to click, delete the 0, and then type 1

ty vm for any help
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,156
#2: Jun 13 '06

re: VB Highlight all text in textbox by clicking


I was under the impression that text (edit?) boxes did this anyway.

However if that is not the case then you should be able to do something like

Implement the OnClick handler

In the handler select all Sel(0, -1) I think


I say this as a C programmer rather than a VB programmer though.
Newbie
 
Join Date: Apr 2007
Posts: 1
#3: Apr 26 '07

re: VB Highlight all text in textbox by clicking


Not sure what you are trying to do and I'll take a simple stab at it.

Lets say you have a control Text1

When text 1 gets the focus of the system, you want to highlight the text, and change the number inside of the control to 1 from 0.

That in itself is real simple with code:

Expand|Select|Wrap|Line Numbers
  1. Private sub text1_GotFocus()
  2.  
  3. 'when control receives the focus, set the back color to color of choice
  4. text1.backcolor =  vbyellow '[vb(color of your choice) OR hex value for color]
  5. 'set the value of the text box to 1
  6. text1.text = "1"
  7.  
  8. end sub
  9.  
The only problem with this code is that you do not have control if you accidently tab into the control from changing the value to 1. I would suggest just doing the data entry manually. If your data is important enough to change, then its also important enough to protect from error. Tab in, change the value with the keyboard.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#4: Apr 27 '07

re: VB Highlight all text in textbox by clicking


Thanks for that, Kolus.

Based on the original question I'd say darkk can use what you've shown, and just skip the text1.text = "1".

darkk, if you are going to highlight the textbox as suggested by Kolus (by setting the background colour, in the example) don't forget to set it back to the original colour in the LostFocus event. Note, if you need to remember the original colour so it can be restored, the .Tag property of the control is a convenient place, though it's a string.

Banfa: Not sure about VB.Net, but in VB6 textboxes certainly do not select all by default. And I, for one, am glad that they don't. It's simple enough to implement if you want, but it would be a nuisance if you had to remember to go in and remove the selection each time.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#5: Apr 27 '07

re: VB Highlight all text in textbox by clicking


Oops!

Sorry, I didn't read Kolus's post in enough detail. I thought the background-colour highlight was being done as well as selecting the text, not instead of it. Try this...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_GotFocus()
  2.  
  3.   ' When control receives the focus, auto-select the complete contents.
  4.   With Text1
  5.     .SelStart = 0
  6.     .SelLength = 1000
  7.   End With
  8.  
  9.   ' You can also set the background colour if you want, of course.
  10.  
  11. End Sub
Note that this is all VB6 code. The details (such as the definition of the Sub) will vary in later versions of VB, but the concepts should still be valid.

Also, where I used 1000 for the SelLength - this can be any number, as long as it is at least equal to the length of the text. You can use the Len() function to set it to the correct length, but anything longer will also work. Sometimes I set it to 65535, just to be sure.
eladkarako's Avatar
Newbie
 
Join Date: May 2007
Location: Israel, Netanya
Posts: 1
#6: Jul 23 '08

re: VB Highlight all text in textbox by clicking


the correct way:

Expand|Select|Wrap|Line Numbers
  1.     ''''''' Text1.SetFocus
  2.     Text1.SelStart = 0
  3.     Text1.SelLength = Len(Text1.Text)
  4.  
Newbie
 
Join Date: Apr 2009
Posts: 1
#7: Apr 20 '09

re: VB Highlight all text in textbox by clicking


Okay, that works when you will always want to replace *everything* in a text box, but what about when sometimes you want to replace everything but other times you want to insert or correct text at a certain point in the text box? For example, when you've entered a search phrase, only to realize you need to modify it to get better results. Here's the functionality I've seen that meets this need:

1st click in the text box - highlights everything
2nd click in the text box - places cursor at the point of the click

This is the way many text boxes work these days, such as the address bar in Mozilla browsers, Google toolbar (for mozilla), etc...

Anybody know how to do this in VB?

-Sam
Newbie
 
Join Date: Oct 2009
Posts: 1
#8: Oct 17 '09

re: VB Highlight all text in textbox by clicking


Private Sub TextBox1_gotfocus() Handles TextBox1.GotMouseCapture
Me.TextBox1.SelectionStart = 0
Me.TextBox1.SelectionLength = Len(TextBox1.Text)
End Sub
Reply