article

A Must See Tips: Real Numeric Character in TextBox. Avoid Paste Alpha Character

Email
Submitted on: 2/2/2015 9:41:00 PM
By: Masino Sinaga (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: VB 5.0, VB 6.0
Views: 701
 
     Many tips (and trick) tell us that if we want the textbox control ignore the character which is not numeric character, then we can just put the code that shown in KeyPress event procedure below. But, sometimes we forgot that although we have put the code in KeyPress event procedure, user still can input the alpha character to textbox control by doing copy and Paste to textbox. So, here is another tips to fix the problem. I hope this helpful.

 
				Private Sub Text1_KeyPress(KeyAscii As Integer)
 If Not (KeyAscii >= Asc("0") & Chr(13) _
 And KeyAscii <= Asc("9") & Chr(13) _
 Or KeyAscii = vbKeyBack _
 Or KeyAscii = vbKeyDelete _
 Or KeyAscii = vbKeySpace) Then
Beep
KeyAscii = 0
End If
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
 If Not (KeyAscii >= Asc("0") & Chr(13) _
 And KeyAscii <= Asc("9") & Chr(13) _
 Or KeyAscii = vbKeyBack _
 Or KeyAscii = vbKeyDelete _
 Or KeyAscii = vbKeySpace) Then
Beep
KeyAscii = 0
End If
End Sub
'If user paste the character which is not
'numeric character, Text1 will ignore it.
Private Sub Text1_Change()
 If Not IsNumeric(Text1.Text) Then
 Text1.Text = ""
 End If
End Sub
'Try Paste some character which is not numeric
'to Text1 and Text2 control (copy alpha character 
'from another file, paste it to those textboxes).
'See the difference between Text1 and Text2!!!
'So, don't forget to add the code in event
'procedure Change belongs to the textbox if
'you want your textbox control avoid the character
'which is not numeric. This is often we forgot!


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.