Disable sounds when pressed enter key to append text on richtextbox - vb.net

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

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

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

VB.NET simulate button click and how do I return it to normal?

I have difficulty with this code, I explain:
I have created a textbox and when I press enter, it simulates clicking on a button.
Sub TextBox1KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
'simulate to press Button
Button1Click(sender,New EventArgs)
Else
'code to return to normal
End If
End Sub
It works fine, but how do I return it to normal? I mean, when I click the button, it does not react.
Please wait for your help with this problem that arises
It is usually not a good idea to call an event procedure. Move your code from the Button1.Click to a separate sub then call it from both places.
Private Sub TextBox1_KeyDown_1(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'simulate to press Button
MyButtonSub()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MyButtonSub()
End Sub
Private Sub MyButtonSub()
MessageBox.Show("Button One was clicked or pretend clicked.")
End Sub

How to stop space bar from clicking button

I'm using VS 2008. In either a Windows or Smart Device project, pressing Space bar key when a button currently has the focus will cause the button to be clicked and it will raise the Click event. Is there any property or setting which will cause it to skip it so that the space bar does not cause a button click?
You can use the Form's KeyPreview property along with the KeyDown event to catch the space bar before it is passed to the button and if the button has focus, then you can mark the key press handled to prevent it from bubbling up:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Button Clicked")
End Sub
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.Space And Button1.Focused Then
e.Handled = True
End If
End Sub