Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the contents of a textbox once it is activated?

I have this simple Userform, where I only have TextBox1 and TextBox2. I enter some text in both of them. Assume the focus is on (the cursor is in) the TextBox2. When I click on TextBox1, I want the whole text in this control to be highlighted (selected). Thus I use this code:

Private Sub TextBox1_Enter()
    With TextBox1
        .SetFocus
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
    MsgBox "enter event was fired"
End Sub

There is a MsgBox at the end which is loaded, that means the event works. However, the text is not highlighted. How to fix this?

I use the Enter event and don't want to use the MouseDown event, because I need the code to also work when the TextBox1 is activated programatically, so I feel the Enter event to be the best choice, as it's fired in both cases! Another drawback of the MouseDown event is: when I click for the second time on the TextBox1, I would not expect the whole text to be highlighted anymore, because the focus was set on the first click and it was not changed after I clicked on the same control for the second time; so in this case I would like the cursor to act normally (not to keep the text marked).

Update
When I click once on the TextBox1, I expect to have this result: enter image description here
If clicked again, the highlight would be removed and the cursor would be placed in the place where it was clicked.

like image 875
ZygD Avatar asked May 08 '15 06:05

ZygD


People also ask

How do I select all text in a text box?

On the Selection Pane, press and hold the CTRL key while clicking each text box that you want to select.

How do I select a textbox in Excel?

Open Microsoft Excel. In the Ribbon, click the Insert tab. On the Insert tab, click the Text option on the far right side, and select the Text Box option.

How do you select all text in access?

Access defines the Ctrl+A combination as "select all records", overriding the more usual "select all text" usage.


2 Answers

Can't be more simple than this I guess...

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
    With TextBox1
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub

Whether you click on the textbox or you tab into it, it will do what you want. To deselect the text, use the arrow keys.

Explanation

If you debug the code you will see that even though you have said .SetFocus, the focus is not on the Textbox. .SetFocus doesn't work in TextBox1_Enter() and you need to have focus for the rest of the code to work. And hence my alternative...

Alternative

You may also like this version :) This overcomes the limitation of using the mouse in the TextBox

Dim boolEnter As Boolean

Private Sub TextBox1_Enter()
    boolEnter = True
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
    If boolEnter = True Then
        With TextBox1
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        boolEnter = False
    End If
End Sub
like image 196
Siddharth Rout Avatar answered Oct 05 '22 03:10

Siddharth Rout


Pff, took me a while. Actually, your code works, but it highlights the text BEFORE the click event happens. So you clicking in the box instantly overrides the selection created by the code. I have used a delayed selection, and it works, though it is a bit disgusting...

The code for the textboxes:

Private Sub TextBox1_Enter()
  Application.OnTime Now + TimeValue("00:00:01"), "module1.SelectText1"
End Sub

Private Sub TextBox2_Enter()
  Application.OnTime Now, "module1.SelectText2"
End Sub

Note that it works even withouth the {+ TimeValue("00:00:01")} part, but it might theoretically stop it from working at times. Hmm, on a second thought, just leave it out. I doubt it would ever cause a problem.

Now the code in module1:

Sub SelectText1()
  UserForm1.TextBox1.SelStart = 0
  UserForm1.TextBox1.SelLength = Len(UserForm1.TextBox1.Text)
End Sub

Sub SelectText2()
  UserForm1.TextBox2.SelStart = 0
  UserForm1.TextBox2.SelLength = Len(UserForm1.TextBox2.Text)
End Sub

Hope this works for you too. Ineresting problem. :) Cheers!

like image 45
vacip Avatar answered Oct 05 '22 03:10

vacip