Having Text Box accept Numbers Backspace and a Decimal place - vb.net

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 = ".")

Related

vb.net textbox no special characters

I would like to skip a part of validation and just make it so the text-box can't have anything I don't want in it.
The intention is to allow backspaces, spaces, and letters : d,r,i (upper and lower) be entered.
How can I make it so that no special characters get entered like {}, !, :;", etc.?
Private Sub txtParty_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtParty.KeyPress
'allows only numbers, letter, space, and backspace
If Char.IsControl(e.KeyChar) = False And Char.IsSeparator(e.KeyChar) = False And Char.IsLetterOrDigit(e.KeyChar) = True And e.KeyChar <> "d" And e.KeyChar <> "D" And e.KeyChar <> "r" And e.KeyChar <> "R" And e.KeyChar <> "i" And e.KeyChar <> "I" Then
e.Handled = True
End If
End Sub
Probably easier with a couple of If-Blocks to filter the data.
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs)
Handles TextBox1.KeyPress
If e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> " " Then
If Not Char.IsLetter(e.KeyChar) OrElse
Not "DRI".Contains(e.KeyChar.ToString.ToUpper) Then
e.Handled = True
End If
End If
End Sub
Of course, you would still have to intercept the Ctrl-V and remove the ContextMenuStrip to prevent pasting text into the TextBox.

passing a function for validating numbers only vb.net

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

How to allow the space key and backspace key in VB.NET/ Visual Studio 2008?

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

Proper Textbox that only allow numeric and decimal in visual basic

I really need help in visual basic.
I have 3 textboxes that allow numeric and decimal with the code below:
If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar)
AndAlso Not e.KeyChar = "." Then
e.Handled = True
End If
But I have encountered few problems:
I can put "." anywhere
when I try to add with each textbox, the result is a mess. example 1.2 + 3 =15
Can you fix my code so it can make a proper decimal calculation?
This can be helpful
Private Sub txtrate_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtmarks.KeyPress
If InStr(txtmarks.Text, ".") Then
e.Handled = Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar))
Else
e.Handled = Not ((Char.IsDigit(e.KeyChar) Or e.KeyChar = ".") Or Char.IsControl(e.KeyChar))
End If
End Sub

allow numbers, dots and backspace and delete in textbox

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