sijpie
Well-known Member
- Joined
- Nov 1, 2008
- Messages
- 4,269
- Office Version
- 365
- Platform
- Windows
- 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:
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:
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:
(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)
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
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)