Anda di halaman 1dari 2

A Must See Tips: Real Numeric Character in TextBox.

Avoid Paste Alpha Print


Character
Email
(About the author)
Submitted on: 8/4/2003 1:25:28
AM
By: Masino Sinaga
Level: Beginner
User Rating: By 4
Users
Compatibility:VB 5.0, VB 6.0

Users have accessed this article


5035 times.

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.

Terms of Agreement:
By using this article, you agree to the following terms...

1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages
that allow it) freely and with no charge.
2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so
is a violation of copyright laws.
3. You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.

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!

Anda mungkin juga menyukai