Solved

HIGHLIGHT TEXT IN TEXTBOX ON USERFORM

Posted on 2014-03-20
4
448 Views
Last Modified: 2014-03-22
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
Comment
Question by:fordraiders
  • 2
  • 2
4 Comments
 
LVL 4

Expert Comment

by:Ledigimate
ID: 39944568
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
 
LVL 3

Author Comment

by:fordraiders
ID: 39945264
Ledigimate,
and this will actually select the text in the textbox ?

If i hit the Backspace key, it will delete the text ?
0
 
LVL 4

Accepted Solution

by:
Ledigimate earned 500 total points
ID: 39946349
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
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39947060
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now