I am trying to write a piece of code in VB.NET 2003 that will only allow NUMBERS and PERIODS but what I am using currently doesn't work. I am trying to keep this as simple as I can to understand it. This is what I have so far:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar <= Microsoft.VisualBasic.ChrW(7) Then
TextBox1.Text = ""
ElseIf e.KeyChar >= Microsoft.VisualBasic.ChrW(9) Then
TextBox1.Text = ""
ElseIf e.KeyChar <= Microsoft.VisualBasic.ChrW(45) Then
TextBox1.Text = ""
ElseIf e.KeyChar = Microsoft.VisualBasic.ChrW(47) Then
TextBox1.Text = ""
ElseIf e.KeyChar >= Microsoft.VisualBasic.ChrW(58) Then
TextBox1.Text = ""
End If
End Sub
If you hit a wrong key, erase the textbox .... But this doesn't work. The key that is not allowed still gets printed in the textbox anyway.
Before I wrote out this If statement, I previously wrote out a Select Case statement with the values I have posted above and a MsgBox would go off saying I hit the wrong key and did not report a MsgBox if I did hit numbers or the period, but after I clicked ok to the MsgBox the wrong letter that is supposed to be not allowed would appear in the textbox anyway.
What can I do to make the keys I dont want in the textbox not get written to the textbox?
Enter your message below
Sign in or Join us (it's free).