Text box selection

  • Thread starter Thread starter Tim Coddington
  • Start date Start date
T

Tim Coddington

I have a form with a text box containing a date formatted as mm/dd/yyyy.
When the box gets focus, I want the mm/dd part to be selected for
replacement. So far, I have the following:

Private Sub TextBox1_Enter()
TextBox1.SelStart = 0
TextBox1.SelLength = 5
End Sub

Private Sub UserForm_initialize()
TextBox1.Value = "01/01/4000"
TextBox2.Value = "04/03/1999"
End Sub

When the form comes up, textbox1 has focus and 01/01 is highlighted. But
when I press <enter>, <tab>, or click on a text box to enter it, it seems
that '_Enter()' doesn't fire at that time.

How can I get the first 5 characters highlighted every time I enter the text
box?
 
Thanks. That helps alot except for when you click on the box to enter it.
Is there any way to make it work then also?
 
Tim

You could use the MouseDown event also, but you'll need a way to limit that
to only when the textbox is first selected. I'm not sure if there are any
problems with this, but here's something you could try:

Dim bEntered As Boolean

Private Sub TextBox1_Change()
bEntered = False
End Sub

Private Sub TextBox1_Enter()
Me.TextBox1.SelStart = 0
Me.TextBox1.SelLength = 3
bEntered = True
End Sub

Private Sub TextBox1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

If bEntered Then
Me.TextBox1.SelStart = 0
Me.TextBox1.SelLength = 3
bEntered = False
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top