Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

HIGHLIGHT TEXT IN TEXTBOX ON USERFORM

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

856 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