How to submit on keypress (vb.net) - vb.net

I have written a login. Now I want to do this (press return to login):
Private Sub login_KeyDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyData = Keys.Return Then
login()
End If
End Sub
It does not work. At this time it works only with a button.

Try using the KeyDown events of the textboxes instead.
Protected WithEvents txtUsername As TextBox
Protected WithEvents txtPassword As TextBox
Private Sub Login_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUsername.KeyDown, txtPassword.KeyDown
If e.KeyData = Keys.Return Then login()
End Sub

Or you could just set the AcceptButton property of your form to be the button you would like to be "pressed" when return/enter is pressed. This is exactly what this feature is here for.

Related

keypress event instead click in vb.net

I'm trying to implement some events using keys instead of clicks, I know that this code:
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav")
End Sub
is playing the sound when i click the picturebox2, but how can you do this using a key ? example:
presing the key "f5" should play the same sound as the click event
so far i tried keyDown like this:
Private Sub PictureBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles PictureBox2.KeyDown
If e.KeyCode.Equals(Keys.F5) Then
My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav") '
End If
End Sub
But it's not working, and the keypress im not sure how to implement it:
Private Sub PictureBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles
PictureBox2.KeyPress
End Sub
How can I do this implementation ?
Set the form properties to Key Preview to True
Under the Form KeyDown Event
Private Sub frm_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
Handles MyBase.KeyDown
If e.KeyCode = Keys.F1 Then
'Enter your Code here
End If
If e.KeyCode = Keys.F2 Then
'Enter Code here
End If
End Sub
This method should be the easiest one

Specific keyboard characters to call specific button actions

When I start my program, and push the specified keys to call my button commands, it does not do anything.
The focus remains on one of the buttons and nothing occurs.
I've tried multiple codes from using KeyDown to KeyPress to codes that include vbKey.
I am very new to vb, so it is very likely that I just do understand what I am doing. :(
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = "/" Then
Call Button1_Click(sender, e)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Close Program
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPMN.Click
'Add +1 to PMN Textbox (txtPMN)
txtPMN.Text = (Val(txtPMN.Text) + 1).ToString()
End Sub
End Class
I would like to simulate the clicking of certain buttons (activate the button code) when I press specific keys on the keyboard. For example: If I press "/" I would like Button1_Click to activate as if I clicked the button with the mouse. Then, if I push my next key ".", I would like Button2_Click to activate.
I do not want to use modifiers like: SHIFT, CTRL, ALT
Please try with keychar like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "/" Then
Call Button1_Click(sender, New EventArgs)
End If
end sub
when use keydown, the keycode for "/" key is OEM, depend the keyboard type, so char(e.keycode) may be not "/"

How to Set Enter is ok on Textbox in vb.net

I have a password textbox (Tbx1) and a Login button (Btn1). What code will i put where so that when the user
inputs password and presses enter, Btn1 clickevents will be exucuted?
Try this:
Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
fun()
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
fun()
End Sub
Private Sub fun()
MessageBox.Show("Enter key pressed")
End Sub
on Form Misc Properties set the accept button to be Btn1. Equally, you can set a cancel button too to be executed whenever the user presses Esc button.
Code version (through the constructor):
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.AcceptButton = Btn1
End Sub

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