so i have this text field where i would like to only enter numberic values
i am able to do this with this piece of code
.KeyPress //using a keypress event
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
the above code works fine but i am wondering if i can pass it as a function and call that function whenever i need it
somthing like so:
private Function Numbers_Only()
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Function
But it doesn't seem to work can somebody show me the correct way of passing this through the function please
You should declare this as a Sub as it doesn't return any value and modifies the arguments passed in.
Private Sub Numbers_Only(e As System.Windows.Forms.KeyPressEventArgs)
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
Alternatively do it as a function:
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
e.Handled = Numbers_only(e.KeyCode)
End Sub
Private Function Numbers_only(keyChar As System.Windows.Forms.Keys) As Boolean
Return (keyChar < System.Windows.Forms.Keys.D0 OrElse keyChar > System.Windows.Forms.Keys.D9) AndAlso keyChar <> System.Windows.Forms.Keys.Back
End Function
I need to make the text box only accept Numbers Decimals and the Backspace. This is what I have so far and it works great for numbers and backspace but I have no idea what to do for periods. So any help would be great.
Private Sub txtlength_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtlength.KeyPress
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ControlChars.Back Or e.KeyChar = ".")
I have a textbox and I am using the following to validate the user can only add numbers, but how can I let the user to write only one "."
Private Sub txtDiagnostic_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtConsultor.KeyPress
If e.KeyChar <> ChrW(Keys.Back) Then
If Char.IsNumber(e.KeyChar) Then
Else
e.Handled = True
End If
End If
End Sub
I was planing to use contains() or something but how can I realize the textbox has
only one dot? or how to allow the user can write a single dot and only one?
add this line before your first IF
If (e.KeyChar.ToString = ".") And (txtDiagnostic.Text.Contains(e.KeyChar.ToString)) Then
e.Handled = True
Exit Sub
End If
try this code good for money purposes
Private Sub LoanFeeTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles LoanFeeTextBox.KeyPress
If Char.IsControl(e.KeyChar) Then
ElseIf Char.IsDigit(e.KeyChar) OrElse e.KeyChar = "."c Then
If LoanFeeTextBox.TextLength = 12 And LoanFeeTextBox.Text.Contains(".") = False Then
LoanFeeTextBox.AppendText(".")
ElseIf e.KeyChar = "." And LoanFeeTextBox.Text.IndexOf(".") <> -1 Then
e.Handled = True
ElseIf Char.IsDigit(e.KeyChar) Then
If LoanFeeTextBox.Text.IndexOf(".") <> -1 Then
If LoanFeeTextBox.Text.Length >= LoanFeeTextBox.Text.IndexOf(".") + 3 Then
e.Handled = True
End If
End If
End If
Else
e.Handled = True
End If
End Sub
How do I allow the space key and backspace key in VB.NET/ Visual Studio 2008?
Sample code:
Private Sub txtname_KeyPress(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles txtname.KeyPress
If e.KeyChar < "A" Or e.KeyChar > "z" And e.KeyChar <> ControlChars.Back Then
e.Handled = True
txtname.Clear()
Else
End If
End Sub
For Backspace use the Asc Function and test for the Hex Value, in this case 8, for the Space you can just test for " "
If Asc(e.KeyChar) = 8 OrElse e.KeyChar = " " OrElse e.KeyChar < "A" OrElse e.KeyChar > "z" Then
e.Handled = True
CType(sender, TextBox).Clear()
End If
If your question is how to check BackSpace and Space the above answer will work. To allow them along with your Text then do something like this
If Not ((Asc(e.KeyChar) = 8 OrElse e.KeyChar = " ") OrElse (e.KeyChar >= "A" AndAlso e.KeyChar <= "z")) Then
e.Handled = True
CType(sender, TextBox).Clear()
End If
Work at Visual Studio 2012:
Private Sub Username_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Username.KeyPress
If Not (Char.IsNumber(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 32) Then
e.Handled = True
End If
End Sub
i have the following code:
Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress
e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")
End Sub
which allows only digits and . in my textbox, however i also need to be able to delete values using the backspace or the delete button.
Is this possible?
Thanks! :)
This is the wrong approach.
It’s universally agreed that restricting the user’s input is bad for the user experience, and you will invariably fail to handle special cases (what about Ctrl+V, for instance? Ah, you forgot about that. Everybody does).
Instead, .NET offers the Validating event for validating user input. You should intercept that event, not the keypress. Do allow users to enter text however they want; in particular, allow them to make mistakes (e.g. mistyping) without interruption – that would be extremely disruptive and not helpful.
Then, once they’re finished (because input focus leaves the control), do an input validation in one go.
While I totally agree with the answer from Konrad Rudolph
(It's really a messy affair to handle the user input in the KeyPress event)
I wish to give an answer at your question.
The MSDN in the KeyPress docs states that The KeyPress event is not raised by noncharacter keys. This means that you don't get the Delete key, but only the BackSpace key. You could handle this situation with this little change to your event handler
Private Sub TxtPStof_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtPStof.KeyPress
if e.KeyChar <> ControlChars.Back then
e.Handled = Not (Char.IsDigit(e.KeyChar) Or e.KeyChar = ".")
end if
End Sub
If Char.IsLetterOrDigit(e.KeyChar) Or e.KeyChar = ControlChars.Back Or e.KeyChar = "." Then
e.Handled = False
Else
e.Handled = True
End If
this code works fine for me.
If Not ((Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Or Asc(e.KeyChar) = 46 Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 127) Then
e.KeyChar = ""
e.Handled = False
End If
try this one:
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
If Not (Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 46) Then e.Handled = True
End If
Private Sub textbox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textbox.KeyPress
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
e.Handled = True
End If
End Sub
This should do what you want it to do.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim NumString = "0123456789.-"
If Not NumString.Contains(e.KeyChar) Then
If Asc(e.KeyChar) <> 8 Then 'BackSpace is allowed
e.KeyChar = ""
End If
Else
If e.KeyChar = "-" Then
If Len(Trim(TxtDcrAmt.Text)) > 0 Then
e.KeyChar = ""
End If
End If
If e.KeyChar = "." Then
If TxtDcrAmt.Text.Contains(".") Then
e.KeyChar = ""
End If
End If
If TxtDcrAmt.Text.Contains(".") Then
Dim TLen As Integer = 0
TLen = TxtDcrAmt.Text.IndexOf(".") + 2
If Len(Trim(TxtDcrAmt.Text)) > TLen Then
e.KeyChar = ""
End If
End If
End If
End Sub
Put this on keypress event. This allows dots and numbers only
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 46 Or Asc(e.KeyChar) > 57 Or (Asc(e.KeyChar) < 48 And Asc(e.KeyChar) > 46) Then
e.Handled = True
End If
End If