?
Solved

HIGHLIGHT TEXT IN TEXTBOX ON USERFORM

Posted on 2014-03-20
4
Medium Priority
?
701 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 2000 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

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Manually copying shapes and their assigned macros one by one to a new location can be tedious, but if you use the Excel utility workbook attached to this article, the process will be much quicker and easier.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

589 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