Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I highlight an empty textbox? 1

Status
Not open for further replies.

EdRev

Programmer
Aug 29, 2000
510
US
I wrote this code to be able to highlight a text box when it gets the focus:

Private Sub txtRa_GotFocus()
With txtRa
.SelStart = 0
.SelLength = .MaxLength
End With
End Sub

But it doesn't work when the textbox is empty and I can't assign any initial value at design time becasuse there is a validation routine involved.

any help will be greatly appreciated.
 
Something similar to the code below may suffice:

Private Sub Text1_Change()
If Text1.Text <> &quot;&quot; Then
Text1.BackColor = vbWindowBackground
End If
End Sub

Private Sub Text1_GotFocus()
If Text1.Text = &quot;&quot; Then
Text1.BackColor = vbHighlight
Else
Text1.BackColor = vbWindowBackground
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)

End If
End Sub

Private Sub Text1_LostFocus()
Text1.BackColor = vbWindowBackground
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top