Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Jun 18, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Keypress Won't Fire When Pasting Into Text Box

Don't put rules for validating text values or formats in the KeyPress event-use the Change event instead. If you "paste" into a text box, the KeyPress event isn't fired and all your validation goes out the window. Also, if you don't carefully put code in the Change event that sets the value of a text box, you'll create an infinite loop:

Private Sub Text1_Change()
	'Append asterisk to text
	Text1.Text = Text1.Text & "*"
End Sub

Here's a better way:

Private Sub Text2_Change()
	Dim lCurr As Long
	'Append asterisk to text
	lCurr = Text2.SelStart
	If Right$(Text2.Text, 1) <> "*" Then
		Text2.Text = Text2.Text & "*"
		'Be kind and don't put the cursor at the front of the 
		Text2.SelStart = lCurr
	End If
End Sub
Joe Karbowski
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date