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
Related
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
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
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
I am working on a Windows Form Application, making a button to show another form and I am trying to make the user wait some time after he clicks the button and after the time passes then the form will open.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
Form3.Show()
Me.Hide()
End Sub
When I use the code above, Form3 is shown before I even click the button.
You need to make sure that you set the Enabled property on the Timer1 component to False in the form designer.
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