Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

HIGHLIGHT TEXT IN TEXTBOX ON USERFORM

another weird problem:

vba userform textbox:

After typing a value in a textbox and then moving around the from.
Coming back to the textbox and i click in the textbox,
Trying to highlight(select) all the text. but this will not work ?



Private Sub TextBox37_Enter()
 With UserForm2.TextBox37
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With

End Sub
0
Fordraiders
Asked:
Fordraiders
  • 2
  • 2
1 Solution
 
LedigimateCommented:
Hi

You can solve it by placing a transparent image control over the textbox.  The image control should have the following properties set:

BackStyle:  0
BorderStyle: 0
MousePointer: 3

In the image control's click event, set the keyboard focus to the textbox.
Add a line of code to the textbox's enter event handler to hide the image control, and make it visible again in the textbox's exit event handler.

Assuming the name of the image control to be Image1, your code would look something like this:

Private Sub Image1_Click()
    UserForm2.TextBox37.SetFocus
End Sub

Private Sub TextBox37_Enter()
    UserForm2.Image1.Visible = False
    With  UserForm2.TextBox37
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub

Private Sub TextBox37_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    UserForm2.Image1.Visible = True
End Sub

Open in new window

0
 
FordraidersAuthor Commented:
Ledigimate,
and this will actually select the text in the textbox ?

If i hit the Backspace key, it will delete the text ?
0
 
LedigimateCommented:
Hi

Yes, it will.  This is how it will work:

The transparent image control will take the initial mouse-click and it's Click event handler will then give the keyboard focus to the textbox.
As soon as the textbox receives the focus, its Enter event handler will hide the image control and select all the text in the textbox.  At this point the user will be able to type in the textbox and delete text normally, and they will be allowed to click inside the textbox because the image control will no longer cover the textbox.
Then, as soon as the user moves the keyboard focus to another control, the textbox's Exit event handler will show the image control again so the image control will cover the textbox again.

Please try it.  Just be sure to make the image control and textbox control the same size, and to place the image control over the textbox control so when the user wants to click on the textbox control, they would click on the image control instead, but to the user it would look like there is no image control because the image control would be transparent, and the mouse pointer would change to an I-beam when it moves over the image control.
0
 
FordraidersAuthor Commented:
where in the world did you ever come up with this ?

Still dont understand why regular code will not do this ?
but ok Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now