devxlogo

Keypress Won’t Fire When Pasting Into Text Box

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 		'text		Text2.SelStart = lCurr	End IfEnd Sub
See also  Small Business Strategies with Venmo
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist