Enter key in KeyDown - vb.net

I just started learning VB recently and I'm trying to get familiar with the methods. Here I'm trying to write the key code to the console each time I press a key.
Private Sub kd(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Console.WriteLine(e.KeyCode)
End Sub
This work for most of the keys, but strangely I can't get Enter to work. One thing I noticed is that whenever I press Enter I also "clicked" one of the buttons I have in the form.
What is happening here?

Keys like Shift, Enter, Tab, Escape are special keys. They need a little different commands.
Private Sub Form2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
MsgBox("Success!")
End If
End Sub
And that button which you are talking about has the focus. Set the focus to the form using SetFocus Control
That should work perfectly!

User Your Controls Key Down Event and Check for Keys to be Enter
Private Sub kd(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
IF e.key=key.Enter Then
<do your code>
Endif
End Sub

Related

Visual Basic e.SuppressKeyPress Does Not Work When Clicking a Button with Button.PerformClick()

I am trying to make my program press a button in the form if the "enter" key is pressed while in a specific text box.
I have used similar code to change focus between text boxes, and it works perfectly fine.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
TextBox2.Focus()
End If
End Sub
In this instance, the Windows noise does not play, and the focus is changed from TextBox1 to TextBox2.
However, I tried to implement the same code while only changing one line.
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Button2.PerformClick()
End If
End Sub
The "e.SuppressKeyPress = True" does not stop the Windows noise from playing.
I have researched this for about three hours now, and have come up empty handed multiple times. I have tried setting the button to the default "AcceptButton", but then when I press enter in ANY text box, then the button is pressed. I only want the button to be pressed if enter is pressed in TextBox2.
I should also mention, simply pressing the button does not make the noise. Only if the user presses "enter" in TextBox2.
EDIT 1 - I have tried to debug as best I can, and it appears to only be happening when the user presses enter.
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
End If
End Sub
Removing the button click from the code (see above) makes the Windows noise stop. I'm completely at a loss here...
I fixed the issue by changingPrivate Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown to Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPressand only using e.Handled = true.
It seems the Performclick command sends your code off the scope of the keydown command, it is processed as a key event nested into a key event, as a SendKeys.SendWait{F6} command in this same context would, for example.
In other words, it's like you need to call SuppressKeyPress another time. I don't know how to do that.

AcceptButton or CancelButton with UserControls VB

I have a Form with a FlowLayoutpanel which adds several UserControls(TextBox+Label) in it. My Buttons(Accept/Close) are UserControls aswell.
How do I say my UserControl-Buttons that they're Buttons so that I can choose them in the Forms Properties as Accept-/CancelButton? Which property do I need for this?
Or how do I use the KeyPressEvent?
Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
MessageBox.Show("Enter key pressed")
End If
End Sub
and
Private Sub Me_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (Control.ModifierKeys And Keys.Enter) = Keys.Enter Then
MsgBox("enter key pressed")
End If
End Sub
aren't triggered when I press Enter in this Form...
thanks in advance :)
This question is ages old, but in case someone comes across this, the proper way to create a UseControl that should act like a button is to implement the System.Windows.Forms.IButtonControl interface. Once you've done that, you can select it as the Cancel/AcceptButton of the form.

VB.NET SetFocus Equivalent for Enter Button?

I have a textbox called clientFirst and, if I press Enter after I'm done typing in this textbox, I want the Enter button to represent a click on btnAdd. How can I make this happen?
Protected Sub btnAdd_Click(sender As Object, e As ImageClickEventArgs) Handles btnAdd.Click
'Do something
End Sub
There's not much code to post, really. :( But I was suggested to use the KeyDown function by Chase Ernst, which seems like a brilliant idea, except that it keeps telling me that a part of it is undefined. Here's the code I was suggested:
Private Sub clientFirst_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles clientFirst.KeyDown
If e.KeyCode = Keys.Enter Then
btnAdd.PerformClick()
End If
End Sub
It keeps telling me that System.Windows.Forms.KeyEventArgs is undefined. I'm guessing there's an Imports class I have to define at the top? Tried System.Windows, System.Object, System.Windows.Forms, System.KeyEventArgs and nothing works!
Thank you in advance! x
Easy way is to use the HTMLForm.DefaltButton property on the form.
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Page.Form.DefaultButton = "TextBox1"
End Sub
I would use something like this:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
Button1.PerformClick()
End If
End Sub
If the key that is pressed is the enter key, then Button1.PerformClick() will fire.

Form keyDown not working?

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control Then
MessageBox.Show("aaaa")
End If
End Sub
As you can see, my form will check for when the control key is pressed down.
But it doesn't work. Why?
I am not near a computer now so I can't test this, but when I have wanted to get key events on a form before, I would set Form1.KeyPreview to True (or something similar).
That works fine. I assume you have other controls in your form. One of them will get the focus, never the form. Keyboard input only goes to the control with the focus.
You can set the form's KeyPreview property to True. The Winforms' way is to override the ProcessCmdKey() method.
You need to set KeyPreview to true when loading the form, it should work then
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
MsgBox(e.KeyChar)
End Sub

VB2010 - KeyDown Events Not Executing

I have a pong type of game and I want the spacebar to pause the game... Here is my code to detect when the space bar is pressed.
Private Sub PongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.Space Then
Application.Exit()
End If
End Sub
For some reason the application is not closing... IS there something wrong with this code that I am not seeing?
Thanks
Make sure to check your form's KeyPreview property.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx