VB Highlight all text in textbox by clicking

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darkk
    New Member
    • Jun 2006
    • 3

    VB Highlight all text in textbox by clicking

    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
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • Kolus
      New Member
      • Apr 2007
      • 1

      #3
      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:

      Code:
      Private sub text1_GotFocus()
      
      'when control receives the focus, set the back color to color of choice
      text1.backcolor =  vbyellow '[vb(color of your choice) OR hex value for color]
      'set the value of the text box to 1
      text1.text = "1"
      
      end sub
      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.

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        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.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          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...
          Code:
          Private Sub Text1_GotFocus()
          
            ' When control receives the focus, auto-select the complete contents.
            With Text1
              .SelStart = 0
              .SelLength = 1000
            End With
          
            ' You can also set the background colour if you want, of course.
          
          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.

          Comment

          • eladkarako
            New Member
            • May 2007
            • 2

            #6
            the correct way:

            Code:
                ''''''' Text1.SetFocus
                Text1.SelStart = 0
                Text1.SelLength = Len(Text1.Text)

            Comment

            • thunksalot
              New Member
              • Apr 2009
              • 1

              #7
              taking it a step futher?

              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

              Comment

              • admerin
                New Member
                • Oct 2009
                • 1

                #8
                Private Sub TextBox1_gotfoc us() Handles TextBox1.GotMou seCapture
                Me.TextBox1.Sel ectionStart = 0
                Me.TextBox1.Sel ectionLength = Len(TextBox1.Te xt)
                End Sub

                Comment

                Working...