VB.NET: How to validate a textbox to not allow decimal values? - vb.net

I would like to know how i can validate a textbox to not allow any decimal values?

this solution i got from this link ( How to allow user to enter only numbers in a textbox in vb.net? )
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
e.Handled = True
End If
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
e.Handled = False
End If
End Sub

You can use the KeyPress event and use the IsNumeric Function to trap the numeric keys.
Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If IsNumeric(e.KeyChar) Then
e.Handled = True
End If
End Sub

If you can, use a MaskedTextBox
Since handling the KeyPress can cause problem with delete/backspace/copy/paste/...

Related

e.Handled in RichTextBox has no effect. On TextBox, it works

The following code eats the Enter key for a TextBox (with "MultiLine" set to True):
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub
The same code for a RichTextBox however doesn't work: The Enter key is not eaten:
Private Sub RichTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub
I don't see where I could have made a mistake.
Does anybody see where this behaviour might come from?
Got it.
I needs to be consumed in KeyDown. Unlike for TextBox:
Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
End If
End Sub

Detect alphabet or numeric and then send them to textbox on a form?

Based on this question,
But in my case i have a form and textboxt,
i need to detect alphabet or numeric and then send them to textbox same as above but on my form not datagrid.
How to do that? thanks
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = Not (Char.IsLetterOrDigit(e.KeyChar) OrElse (e.KeyChar = ChrW(Keys.Back)))
End Sub

How to use ASCII code for Enter in vb.net

I am having a little problem getting my code to do what I want.
I want to prevent the user from using the Enter button when he/she is entering text into a text. The code I am using is:
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = False
Else
e.Handled = True
MsgBox("Error.")
End If
End Sub
This not achieving my objective. Please how can I re-write this?
I agree with Tim3880. You are indeed keeping the user from entering anything with his/her keyboard; except the enter value. Your code is okay; only wrongly arranged, friend.
Try this:
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 13 Then
e.Handled = True
MsgBox("Error.")
Else
e.Handled = False
End If
End Sub

KeyPress Event in Visual Basic?

I think this is possible, but I don't know... I want to check when the key A is pressed to move the player to the left, but for now just a messagebox.
Here is all the code I could find on the internet, it didn't work...
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.A Then
MsgBox("Left")
End If
End Sub
I am not asking how to do things when you enter stuff in a textbox, I'm asking how to run a event when you press a key.
Try this...
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 97 Or Asc(e.KeyChar) = 65 Then
MsgBox("hello")
End If
End Sub
This should work. 97 is 'a' and 65 is 'A' in ASCII.
Try this
Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyCode = Keys.A Then
MsgBox("Left")
End Sub
or
Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _
Handles textBox1.KeyDown
If e.KeyCode = Keys.A Then
MsgBox("Left")
end if
End Sub
You probably want to set the form's KeyPreview property to true.
Doing this makes sure the form sees all key events even if one of
its child controls
Use the KeyEventArgs.KeyData property to see what key was pressed.
KeyCode , KeyData and KeyValue are members of
System.Windows.Forms.KeyEventArgs in the KeyUp and KeyDown events
only.
They actually simplified this from VB6.The new way with VB 2017 is...
Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtProblem.KeyPress
If e.KeyCode = "a" or e.KeyCode = "A" Then
MsgBox("Left")
End If
End Sub
So, I got one step in the right direction maybe it will help. I'm trying to give control of the paddle to the player in a game of pong and found if textbox1 is highlighted or "has focus" then these commands will execute and I can move the paddle around the screen. The problem is there is also a textbox filling up with letters in the corner of the screen as this happens I'm not sure how to get rid of just yet. Perhaps it will be useful though.
Private Sub
Textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 97 Then 'a
Paddle.Location = New Point(Paddle.Location.X - 10, Paddle.Location.Y)
End If
If Asc(e.KeyChar) = 119 Then 'w
Paddle.Location = New Point(Paddle.Location.X, Paddle.Location.Y - 10)
End If
If Asc(e.KeyChar) = 100 Then 'd
Paddle.Location = New Point(Paddle.Location.X + 10, Paddle.Location.Y)
End If
If Asc(e.KeyChar) = 115 Then 's
Paddle.Location = New Point(Paddle.Location.X, Paddle.Location.Y + 10)
End If
End Sub
Also, you can use this command to give focus to the textbox from a different event
Public Sub ControlSetFocus(control As Control)
If Control.CanFocus Then
control.Focus()
End If
End Sub
This works for me
If e.KeyChar = Convert.ToChar("a") Then
MsgBox(Convert.ToChar("a") +"enter key pressed ")
End If
Also you can use numbers and uppercase in ""

focus on the next text box if KeyCode=.(DOT)

I have 4 textboxes which are used to store IP Addresses. I want to automate the selection of the textboxes by inputting the .(DOT) character; I want also this character to be deleted right after having been input. I have been able to get the auto-selection functionality, but not the character deletion part. Here is my code:
If e.KeyCode = Keys.OemPeriod Then
TextBox2.Focus()
End If
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Asc(e.KeyChar) = 46 Then 'It does not matter how you select the character
TextBox2.Focus()
e.Handled = True 'To avoid the character to be written (i.e., delete the DOT)
End If
End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
If Asc(e.KeyChar) = 46 Then
TextBox3.Focus()
e.Handled = True
End If
End Sub
Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
If Asc(e.KeyChar) = 46 Then
TextBox4.Focus()
e.Handled = True
End If
End Sub