Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text box inconsistent behavior - Word VBA form

Status
Not open for further replies.

mintjulep

Technical User
Aug 20, 2004
1,552
JP
I want to have the last character of the text selected when I ENTER the text box. Seems like it should be simple. However the behavior is inconsistent.

To demonstrate, simple form with two textboxes and a commandbutton.

Code:
Private Sub CommandButton1_Click()
With TextBox1
    .SelStart = Len(TextBox1.Value) - 1
    .SelLength = 1
End With
TextBox1.SetFocus
End Sub
-------------
Private Sub TextBox1_Enter()
With TextBox1
    .SelStart = Len(TextBox1.Value) - 1
    .SelLength = 1
End With
End Sub
-----------------
Private Sub UserForm_Initialize()
TextBox1.Value = "ABCD"
End Sub

Tabbing into textbox1 results in all of the text being selected.

Clicking into textbox1 results in no selection at all.

Clicking commandbutton1 results in the desired situation, tb1 with focus, last character selected.

What am I missing?
 
Hi mintjulep,

To sort out the tabbing issue, change the EnterFieldBehaviour property of the Textbox to fmEnterFieldBehaviorRecallSelection (1).

The mouse click is more difficult - clicking with the mouse (a) enters the field if not already in it and (b) makes a selection from what is in the field. You can catch the mouse event but I'm not sure you can easily determine whether it has been used to enter the field or whether the selection was already there.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top