Click here to Skip to main content
14,836,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sample codings to restrict special characters in vb6 & vb.net?
Posted
Updated 1-May-10 5:11am
v2

They are not the only way, one od the ways to do them...

VB6: Use Ascii values the way you want! Ex:
VB
Option Explicit

'This will only accepts the alphabets and no other key is allowed.
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not KeyAscii >= 65 And KeyAscii < 90 Then
        KeyAscii = 0
    End If
End Sub


VB.NET: Use KeyPressed for validation...
'Textbox KeyPress
' Allow number 0-9 plus backspace, Del + Home + End will be accepted also
Dim ValidInputChar = "0123456789." + vbBack
If not ValidInputChar.Contains(e.KeyChar) then
    e.KeyChar=Nothing           
End If
   
Comments
IndrajitDasgupat 1-Mar-11 8:32am
   
But does not allow to paste
This function restrict user from inserting special characters

if ((!char.IsLetterOrDigit(e.KeyChar))&&(!(e.KeyChar==(char)Keys.Back)))
{
e.Handled = true;

}
else
{
e.Handled = false;

}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900