Solved

HIGHLIGHT TEXT IN TEXTBOX ON USERFORM

Posted on 2014-03-20
4
459 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

14 Experts available now in Live!

Get 1:1 Help Now