Click to See Complete Forum and Search --> : I want to be able to write juste numbers in a textbox


gibea00
February 3rd, 2003, 08:47 AM
Hi

In one of my textbox, the only thing i want that the user can write is a number. Anything else have to be bloc. I thought to return bakcspace if the user write a letter or something else, but i don't know how. Is anybody knows it?

here's my code

Dim intKeyAscii As Int32
intKeyAscii = Asc(e.KeyChar)

txtDateComptable.Text = ""
txtNoReference.Text = ""

If intKeyAscii = 13 Then
Me.btnTrouver_Click(Me, e)
ElseIf (intKeyAscii < 48 Or intKeyAscii > 57) then
'return backspace
End If

Thanks

DdH
February 3rd, 2003, 04:15 PM
You must implement the KeyPress event on the Textbox.
There you can control which characters you want to disable in a the Textbox.

for example, you have a Textbox called Textbox1
then the following code will only allow 0,1,2,3,4,5,6,7,8,9 and backspace in that textbox.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Select Case e.KeyChar
Case "0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c
' All valid character for use in Textbox1
' Do Nothing

Case Microsoft.VisualBasic.ControlChars.Back
' BackSpace
' Do Nothing

Case Else
' Ignore all others
e.Handled = True

End Select
End Sub

Iouri
February 4th, 2003, 10:03 AM
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If Char.IsDigit(e.KeyChar) Then
MsgBox("Digit entered")
End If
'disable non-digits
e.Handled = Not Char.IsDigit(e.KeyChar)

End Sub

BJG
February 6th, 2003, 03:52 PM
You could also try using a Masked Edit Box, which to the user is the exact same thing. Then set the Mask of the Masked Edit Box to only allow a numeric value. I hope this works.