Positioning the cursor in a VBA Textbox

Bengt

Active Member
Joined
Mar 4, 2008
Messages
267
Hello all,
I saw a very old thread on this subject (from 2002) but I think that the question at that time didn't get a comprehensive response. My question is simply this: I want to - programatically - position the cursor in a textbox, at a specific position in the string. Can I do that? In other words, if you have your cursor in a textbox, type some text, move your cursor back and forth and then leaves the textbox, is there then a way to find out, when you get back to the textbox again, the position in which your cursor was positioned when you left the textbox the previous time?

Hope my question makes sense.

Bengt
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi,
Use this to restore the last edit position in textboxes for Enter / Tab / Shift-Tab navigation:
Rich (BB code):
Private Sub UserForm_Initialize()
  Dim x As Control
  For Each x In Me.Controls
    If TypeOf x Is MSForms.TextBox Then
      x.EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection
    End If
  Next
End Sub
TextBox1.SelStart returns/sets the zero-based start position of selection in TextBox1

Regards
 
Last edited:
Upvote 0
Hi,
Thanks a lot for your answer. I was out of town so I didn't see it until now. Tried it though. Works fine. However, a question: I understand that the code snippet changes the behavior of the textbox. But what kind of function is the fmEnterFieldBehaviorRecallSelection? Are there other such functions and where can one learn more about that?

Also, I wonder what the behavior of Selstart would be without that function. I tried your suggestion with your proposed code and without it, and it seemed to me that both ways worked. Why is that?

Do you have to affect the behavior of all textboxes on your form, or could you change just one, if you refer to it by its name?

Bengt
Hi,
Use this to restore the last edit position in textboxes for Enter / Tab / Shift-Tab navigation:
Rich (BB code):
Private Sub UserForm_Initialize()
  Dim x As Control
  For Each x In Me.Controls
    If TypeOf x Is MSForms.TextBox Then
      x.EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection
    End If
  Next
End Sub
TextBox1.SelStart returns/sets the zero-based start position of selection in TextBox1

Regards
 
Upvote 0
Code:
x.EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection
New to me ...
 
Upvote 0
The info about EnterFieldBehavior property can be found in VBE help:

  1. Select any Textbox
  2. Type F4
  3. Find EnterFieldBehavior property and select its combobox
  4. Press F1 to read the help about this property

It says that property "specifies the selection behavior when entering a TextBox or ComboBox ...
The EnterFieldBehavior property controls the way text is selected when the user tabs to the control, not when the control receives focus as a result of the SetFocus method. Following SetFocus, the contents of the control are not selected and the insertion point appears after the last character in the control's edit region"

You can set this property programmatically as well as manually at design of the form.
Behavior can be changed for just one Textbox manually (see p.3 above),
or via code like this: Me.TextBox1.EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection
 
Upvote 0
Also, I wonder what the behavior of Selstart would be without that function. I tried your suggestion with your proposed code and without it, and it seemed to me that both ways worked. Why is that?
Without .EnterFieldBehavior = fmEnterFieldBehaviorRecallSelection you can read .SelStart but can't set it in the TextBox_Enter event at Tab/ShiftTab/Enter navigation.

Put one command button and two textboxes onto the new created Userform .
Copy this code and paste it in the UserForm's module, then try Tab/ShiftTab/Enter navigation to see that the code of TextBox2_Enter does not play.

Rich (BB code):
' Put some values to the textboxes at initializing
Private Sub UserForm_Initialize()
  TextBox1.TabIndex = 0
  TextBox1 = "Text1"
  TextBox2 = "Text2"
End Sub
 
' It works well from the command button's code
Private Sub CommandButton1_Click()
  TextBox2.SetFocus
  TextBox2.SelStart = 2
End Sub
 
' But this does not work at Tab/ShiftTab/Enter navigation
Private Sub TextBox2_Enter()
  TextBox2.SelStart = 2
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,519
Messages
6,160,298
Members
451,636
Latest member
ddweller151

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