Setting cursor to the end of the text in a textbox also when entering with a mouseclick

sijpie

Well-known Member
Joined
Nov 1, 2008
Messages
4,269
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
This is not a question, but demo of a method that works and is simple.

When checking for methods to set the cursor to a particular position in a textbox, you will see the method:
Rich (BB code):
Private Sub Textbox1_Enter()
     Textbox1.SelStart = 5
End sub

for instance to set the cursor after the 5th character of the text in the textbox.

That code works fine on one condition: that the code is being run from say a button click.

If you want to have the code run also when the user enters the textbox using the tab key, you will need to set the textbox EnterFieldBehaviour to

1fmEnterFieldBehaviourRecallSelection

However, if the user enters the textbox with a mouseclick, as most people will do, then if the user clicks somewhere in the existing text to enter the textbox, the cursor will stay there and not move to the end.

To take care of this you will need to also set up the TextBox_MouseDown event

So the code to set the cursor to the end of the text in the textbox will be:
Rich (BB code):
Private Sub Textbox1_Enter()
     Textbox1.SelStart = Len(Textbox1.Text)    'put cursor at end (when entering with tab)
End sub

Private Sub TextBox1_MouseDown(ByValButton As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y AsSingle)
    Textbox1.SelStart = Len(Textbox1.Text)    'put cursor at end (when entering with mouseclick)

End Sub
And set the property of the EnterFieldBehaviour to 1


The Textbox1_Enter()sub is processed first, before the Mousedown sub.

So for instance,in one of my forms I add a new line and today’s date in a log field when the user enters this field (multiline textbox). I add the code for this in the Textbox_enter sub. Here the textbox has been given the name tb_Progress:
Rich (BB code):
Private Sub tb_Progress_Enter()

    tb_Progress.Text = Trim(tb_Progress) & vbCrLf& Format(Date, "Short Date") & ": "  
    tb_Progress.SelStart =Len(tb_Progress.Text)    'put cursor at end when user tabs into textbox
End Sub

Private Sub tb_Progress_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal XAs Single, ByVal Y As Single)
    tb_Progress.SelStart =Len(tb_Progress.Text)    'put cursor at end  when user clicks into textbox

End Sub



(to use this code you will need to add some more code to remove ‘empty’ dates in case a user clicks in the field, clicks away again and clicks back in the field)
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top