How do I use key_down event in my vb.net? I have seen so many codes on google but none of them working I don't understand where its going wrong
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx#Y600
http://www.daniweb.com/software-development/vbnet/threads/114278
http://forums.devshed.com/net-development-87/keypress-allow-only-letters-and-numbers-with-max-lenght-of-528176.html
http://social.msdn.microsoft.com/Forums/pl-PL/Vsexpressvb/thread/aab1d64c-a9dc-4dd2-8d2f-83a414e9c909
http://www.bigresource.com/VB-Lock-textbox-so-the-user-only-can-enter-numbers-a8mxDB7ouq.html
http://forums.devx.com/archive/index.php/t-96951.html
Above are the links I googled and they are many links =I have googled to work on key_down events on vb.net But my god its aint working I dont know what to do.
1) How do I use textbox1_keydown event are there any necessary steps taken before using it ( why its aint workin?)
2) can anyone post a sample of these "a textbox that allows only numbers from user using key_down"
3) I see people using e.keychar but in my vb.net(2008) I dont have that keyword I guess e.keycode must be used is that right? or e.keyvalue?
4) I see e.keycode = keys.A but I need to accept 'a' not "A" How do I specify a number or letter with their ascii value ?
Are you not able to use KeyPress for some reason?
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If Char.IsDigit(e.KeyChar) Then
e.Handled = True 'Prevents all numbers from from being placed
End If
If Convert.ToInt32(e.KeyChar) = 97 Then
e.Handled = True 'Prevents "a" from being placed
End If
End Sub
This should do what you need. You can find a list of ASCII codes here if you don't have one already. Take a look at the intellisense for Char. and check out all the useful things you can filter on. You can also specify exact ASCII values by converting them to int.
It sounds like you are getting a little confused between KeyDown and KeyPress.
I think this is what you are looking for:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Not Char.IsNumber(e.KeyChar) Then
e.Handled = True
End If
End Sub
If you are trying to enter only numbers and the lower case "a" then:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress
If Not (Convert.ToInt32(e.KeyChar) = Asc("a") Or Char.IsNumber(e.KeyChar)) Then
e.Handled = True
End If
End Sub
Related
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
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
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 ""
I have a text box where phone number must be entered I want to limit the digits to be entered in the text box only upto 8 places how to do it?
You can subscribe for the KeyDown event to see which key is pressed, then just allow Numeric & backspace key
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.MaxLength = 8
AddHandler TextBox1.KeyDown, AddressOf HandleTbKeyDown
End Sub
Private Sub HandleTbKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If Not ((e.KeyValue >= 48 AndAlso e.KeyValue <= 57) OrElse e.KeyValue = 46) Then
e.Handled = True
End If
End Sub
For text box having only numbers you can do by this:-
if(!((e.keyCode>=48&&e.keyCode<=57)||(e.keyCode==46)))
Also you can check the length to 8 like
Texbox MaxLength = 8
Use a MaskedTextBox. See this link. Though the example is for C# it will work similarly. Your Mask property will be 00000000
for e.g.
myMaskTextBox.Mask = "00000000";
You can use it format data for dates, currencies etc. Also there is a handy BeepOnError property. Again see at the end of example.
I am trying to validate wether a number is a decimal in Visual Basic. The results I get when the number is valid the msgBox shows. When it is not valid, I don't receive the msgBox and the program crashes with an error message that number has to be less than infinity.
I tried adding another If Not IsNumeric(txt1.text) then -- But received the same results.
Where did i go wrong?
If IsNumeric(txt1.text) Then
msgBox("good")
Else
msgBox("not good")
End If
Try using Double.TryParse or Decimal.TryParse instead of IsNumeric.
Dim result as Double = 0.0
if Double.TryParse(txt1.text, result) then
' valid entry
else
' invalid entry
end if
I have just had to write a function which restricts input to a text box to valid decimal values, and I came up with the following:
Private Sub validateDecimalTextBox(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) handles myTextBox.keyPress
Dim textBox As TextBox = DirectCast(sender, TextBox)
If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And textBox.Text.IndexOf(".") < 0) Or (e.KeyChar = "-" And textBox.Text.Length = 0)) Then
e.Handled = True
End If
End Sub
This should restrict user input to decimal values, allowing negative values as well.
If you restrict the user inputs then when you get the value out from the text box you can be more confident that it is valid.
This solution is not complete however as it would allow a user to enter just "-" in the text box which would (presumably) not be a valid input for you. Therefore you can use the solutions that others have mentioned and use any of the following in a sensible way.
double.parse,
double.tryparse
isNumeric()
My personal preference would be for isNumeric() but the choice is really up to you.
You can ignore characters in the textbox's keypress event, like:
Private Sub txtValue_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress
If Not Char.IsDigit(e.KeyChar) Then
If Not (e.KeyChar = vbBack) Then
e.Handled = True
End If
End If
End Sub
not sure which version of VB you're using, assuming it's .NET
You can also use Textbox Keypress event. i.e
Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Textbox1.KeyPress
If (e.KeyChar < "0" Or e.KeyChar > "9") And e.KeyChar <> "." And e.KeyChar <> ControlChars.Back Then
e.Handled = True
Else
If e.KeyChar = "." Then
If Textbox1.Text.Contains(".") Then
Beep()
e.Handled = True
End If
End If
End If
End Sub
I hope this helps.