keypress event instead click in vb.net - 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

Related

How to link a key, e.g "A", to a button?

I am creating a game in VB and I have 4 images and 4 buttons down each image, of course right now I can click the button with the mouse to pick an option but I want to type the letter "A", "S", "D","F" of the keyboard to activate each button.
How can you do this in VB?
My current code for the button is the following
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Computer.Audio.Stop()
My.Computer.Audio.Play ("D:\VisualStudio\DIFgame\sounds\correctSound.wav") 'correct sound audio
Cara.Visible = True 'show face
End Sub
You should set the KeyPreview property (documentation) of the Form to True, then in the Form's KeyDown event (documentation) you would check the incoming KeyCode. If it matches one of your desired shortcuts, then raise the event of the button.
Something like:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If (e.KeyCode = Keys.A) Then
RaiseEvent Button1.Click
End If
End Sub

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 "/"

Disable sounds when pressed enter key to append text on richtextbox

I have this in my design form.
My codes are the follwoing
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
Call Button2_Click(sender, e)
End If
End Sub
The problem is when I click on textbox, typed something in it then pressed enter I will hear a beeping sound. That sound is the one that I want to disable.
I also notice that if I just typed in the textbox and click the button I can hear no sound, the sound occurs whenever I clicked on textbox, type something in it and pressed enter.
EDIT:
By conducting a thorough research, I realized that the ding sounds doesn't come from button pressed but from the last line of richtextbox. It is the same sound that we can heard when we pressed key_down and we are in the last line of a richtextbox. How do I disable it?
Just set the AcceptButton() property of the Form to Button2. Then you don't need the KeyUp code at all:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AcceptButton = Button2
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
' Don't need this code anymore:
'Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
' If e.KeyCode = Keys.Enter Then
' Call Button2_Click(sender, e)
' End If
'End Sub

How can I call keyDown event by passing arguments, Winforms Vb.net

The Following is a combo box keydown event
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
I would like to trigger same event from combobox_leave by passing 'enter key' I did as follows but not working, how to achieve this?
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1_KeyDown(Me, Keys.Enter)
End Sub
Why not just extract the method from the actual event?
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
performAction(e.KeyCode);
End Sub
Private Sub performAction(e as Keys)
If e = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
performAction(Keys.Enter);
End Sub
You could also use the SendKeys.Send Method
When the user leaves the Combobox (like in your example),
You could set back the Focus to the combobox
and then use SendKeys.Send("{ENTER}") to trigger the enter keydown.
much like this:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Focus()
SendKeys.Send("{ENTER}")
End Sub
However this prevents users from focusing to another component. To prevent this, you could use an if statement that if the user clicks or focuses on another component after focusing on the combobox, the user can still "leave" the combobox.
Your kind of approach is not advisable and this leads to a misunderstanding in the part of the user.
try this :
Private Sub ComboBox1_KeyDown(sender As Object, e As
keyEventArgs) Handles ComboBox1.KeyDown
Dim _KeyCode As Short
If e Is Nothing Then
_KeyCode = 13
Else
_KeyCode = Keys.Enter
End If
If _KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs)
Handles ComboBox1.Leave
Dim keypress As System.Windows.Forms.KeyPressEventArgs
ComboBox1_KeyDown(sender, keypress)
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.