How to prevent character to be showed?

alexk

Freshman
Joined
Jul 23, 2002
Messages
34
Location
Israel
How to prevent the User from writing some characters in a TextBox.
For example: If User try to type digits - the TextBox will stay clear.

In VB6 I did it very simply: KeyAscii = 0 in TextBox_KeyPress event, but how do it in VB.NET ???
Thanks.
 
Set the Handled property of the KeyPressEventArgs (e) to True, to
show that the keypress has been handled by your code.

Visual Basic:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  If Char.IsDigit(e.KeyChar) Then
    e.Handled = True
  End If
End Sub

Keep in mind that if the user copies a number and pastes it into
the textbox, the number will slip through this protection, so you
may want to reconsider how you work it if this turns out to be a
problem.
 
Thanks.
It's work good.

The 'Paste' problem is solved very simply: Clear Clipboard when TextBox get Focus :)
 
Damn, I can't imagine anything more annoying than someone's program clearing your clipboard.
 
Back
Top