SetFocus in user form

  • Thread starter Thread starter John Tjia
  • Start date Start date
J

John Tjia

I am creating a user form with a text box that starts with the number
0. I've got the userform_activate to set textbox1.setfocus so that
the cursor is in the text box. However, I find that the cursor is
blinking at the end of the 0, so that the user has to tap to the front
and then go forward to highlight the 0 in order to put in a new
number. How can I set it so that the 0 is highlighted right away, and
the user can input the right number over the 0 immediately?

Many thanks for advice!
 
Try this one John

Private Sub UserForm_Activate()
With Me.TextBox1
.SelStart = 0
.SelLength = Len(.Text)
.SetFocus
End With
End Sub
 
Private Sub TextBox1_Enter()
With TextBox1
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub
 
John,

I'm not sure about the highlighting aspect of your question, but here's what
you can do to shift the insertion point to the left of the zero:

With UserForm1.TextBox1
.Text = "0"
.SetFocus
.SendKeys{LEFT}
End With

-- Dennis Eisen
 
Thanks to all! The .SelStart = 0, .SelLength = Len(.Text) code is
great.

Dennis, I tried your code, but VBA didn't accept the .SendKeys{LEFT}.
I saw something once that was the equivalent of a Tab command in the
userform, i.e., set the focus on the next item in the tab order, and
then do a Shift+Tab equivalent to back up. Thanks for the reply.
 
John,

I was (incorrectly) trying to recall from
memory the form of the SendKeys that would do the shifting. The correct form
of the instruction is as follows, and will
result in "AB|C" showing up in the
TextBox, rather than "ABC|".

Sub ShiftLeft( )
UserForm1.TextBox1.Text = "ABC"
Application.SendKeys ("{LEFT}")
UserForm1.Show
End Sub

And to be precise about the .SelLength and .SelStart properties, you'd want to
use .SelStart = Len(.Text) - 1 and
..SelLength = 1 to get you to shift the
insertion point one character to the left
and highlight the rightmost character.

-- Dennis Eisen
 
Dennis later said in a separate thread:

I've been wrestling with this one for days, and I just can't
seem to pull it off. What I'd like to do when a certain TextBox
on a UserForm takes the focus is to have the insertion point
automatically move one space to the left. So, for example,
if the TextBox contains ""ABC" upon taking the focus what
I want to see is "AB|C"; I've been trying to do this by using
Application.SendKeys "{LEFT}" in some manner but to no
avail. I can obviously do it manually by pressing the left arrow
key, but I want to do it in VBA so as to remind the user of how
the data entry appearing in that particular TextBox is to be
modified. Anyone have any ideas on how to do this?

-- Dennis Eisen

so even he doesn't appear to be still enamored with his suggeste solution.
 
Back
Top