MrExcel Publishing
Your One Stop for Excel Tips & Solutions

HIGHLIGHTING TEXT - In A TextBox


Posted by Randy Parris on November 12, 2001 4:26 AM


Hi There,

Could someone check the code below and give me a hand please. I have 2 textboxes sitting on a label. The lable has a decimal point and textbox1 is infront while textbox2 is behind. This is being used to make up a premium. On the user form I have place 0 in textbox1 and 00 in tbox2, so that everytime the form is used the zeros would be there. As you will see by the following code I have it set up that when the user enters the decimal point it skips to tbox2. With the rest of code the 00 is to be highlighted but the cursors skips to the back of the 00 and does not hightlight them. Can anyone offer any assistance?

Thanks
Randy

I have others like this so I copied all the code instead of having to type it

Private Sub Reserve1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

If KeyCode = 110 Then
KeyCode = 0
Reserve2.SetFocus
End If
End Sub
Private Sub Reserve2_Change()
Reserve2.Text = Replace(Reserve2.Text, ".", "")
End Sub

Sub Form_Load()
Payment1_LostFocus
Payment2_LostFocus
Receipt1_LostFocus
Receipt2_LostFocus
Reserve1_LostFocus
Reserve2_LostFocus
End Sub
Sub SetSelected(ctl As TextBox)
ctl.SelStart = "0"
ctl.SelLength = Len(ctl.Text)
End Sub
Private Sub Payment1_LostFocus()
Call SetSelected(Payment1)
End Sub
Private Sub Payment2_LostFocus()
Call SetSelected(Payment2)
End Sub
Private Sub Receipt1_LostFocus()
Call SetSelected(Receipt1)
End Sub
Private Sub Receipt2_LostFocus()
Call SetSelected(Receipt2)
End Sub
Private Sub Reserve1_LostFocus()
Call SetSelected(Reserve1)
End Sub
Private Sub Reserve2_LostFocus()
Call SetSelected(Reserve2)
End Sub


Posted by Rick E on November 12, 2001 10:17 AM

You have been asking this for some time now. Instead of SetFocus try SendKeys:

If KeyCode = 110 Then
KeyCode = 0
SendKeys "{Tab}"
' comment out Reserve2.SetFocus
End If
End Sub

This will send the focus to the next object which should be you next text box (Reserve2) This is the same as entering the "Tab" key, after entering the first value.