How to handle KeyDown in VB.NET - vb.net

Ok here's my dilemma. here's this code I have:
If e.KeyCode = Keys.A Then
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
Now when I enter this under Form1_KeyDown, visual basic thinks this:
'KeyCode is not a a member of 'System.EventArgs'
Now I've seen this code work before, but it isn't here. Any help?
Here's the full code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
If e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub

Not sure why you method definition declares e as EventArgs, but the fix is simply to make the parameter of type KeyEventArgs. This is because EventArgs (naturally) does not contain a property called KeyCode, but KeyEventArgs does!
Change your event handler method definition to the following:
Private Sub foo_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.A Then
TextBox1.AppendText("A, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
ElseIf e.KeyCode = Keys.S Then
TextBox1.AppendText("C,")
PictureBox14.Visible = True
My.Computer.Audio.Play(My.Resources.D, AudioPlayMode.Background)
End If
End Sub

It sounds like your method is using the wrong EventArgs. The Control.KeyDown event sends it as a System.Windows.Forms.KeyEventArgs. So your code should read as
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs)
// code here
End Sub

I've found sometimes you need to provide the full object type similar to the below:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
MsgBox(e.KeyCode.ToString()) 'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A") 'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub
Have you also tried testing this in a textbox on the form similar to the below (textbox in example is named TextBoxKeyTest)?
Private Sub TextBoxKeyTest_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBoxKeyTest.KeyDown
MsgBox(e.KeyCode.ToString())'Message what the keycode
If (e.KeyCode = Keys.A) Then
MsgBox("e.KeyCode = Keys.A")'Message that I've found the A
TextBox1.AppendText("C, ")
PictureBox2.Visible = True
My.Computer.Audio.Play(My.Resources.C, AudioPlayMode.Background)
End If
End Sub

This should work in VB 2010
Private Sub cmdWiden_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Up Then Me.Top = Me.Top - 5
If e.KeyCode = Keys.Down Then Me.Top = Me.Top + 5
If e.KeyCode = Keys.Left Then Me.Left = Me.Left - 5
If e.KeyCode = Keys.Right Then Me.Left = Me.Left + 5
End Sub

Related

Return the currently selected object

The code says everything, if I press F1 and I'm already with Button1 selected, then it should select Button2 and vice-versa.
If keyData = Keys.F1 Then
If Button1.Select() = True Then
Button2.Select()
ElseIf Button2.Select() = True Then
Button1.Select()
Else
Button1.Select()
End If
End If
But the expression "Button2.Select() = True" doesn't return a value.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.F1 Then
If ActiveControl.Name = "Button1" Then
Button2.Select()
Else
Button1.Select()
End If
End If
End Sub
Actually, F1 is a poor choice because it is traditionally the "Help" key.
We can use form keydown (set form keypreview property to true) or use button previewkeydown like these:
Private Sub Button1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button1.PreviewKeyDown
If e.KeyCode = Keys.F1 Then
Button2.Focus()
End If
End Sub
Private Sub Button2_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Button2.PreviewKeyDown
If e.KeyCode = Keys.F1 Then
Button1.Focus()
End If
End Sub

Windows form hotkeys

I'm trying to use hotkeys on a form to hide/show a textbox, tried many ways and after reading this thread, I did this:
If Control.ModifierKeys = Keys.B Then
If TextBox1.Visible = True Then
TextBox1.Visible = False
Else
TextBox1.Visible = True
End If
and:
If Control.ModifierKeys = Keys.B Then
If TextBox1.Visible = True Then
TextBox1.Hide()
Else
TextBox1.Hide()
End If
End If
but still not working.. help me, please
First you need to set the forms property
KeyPreview = True
Then use the forms KeyDown event
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.B Then TextBox1.Visible = Not TextBox1.Visible
End Sub
If you want to use an ALT or other combination you need to check the modifiers too
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If CBool(e.Modifiers And Keys.Alt) AndAlso e.KeyCode = Keys.B Then TextBox1.Visible = Not TextBox1.Visible
End Sub
If you want to do lots of them then use a select case statement
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
case Keys.B : TextBox1.Visible = Not TextBox1.Visible
'etc
End Select
End Sub

VB.Net detect keydown on control before keydown on form event

I have enabled the KeyPreview option on the form and on the keydown of the form I am detecting the Escape keydown to popup a msgbox requesting whether the user wants to exit from the application.
But the problem is, I have one textbox that needs to clear its contents on Escape keypress rather than asking whether the user needs to exit. And I tried using the keydown event on this control but the form keydown event occurs before the control keydown (I want this to happen the other way around).
Code on the Textbox
Private Sub txtAmount(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Code used on the Main menu Form
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
I can't use the previewkeydown either because the MaskTextBox doesn't raise the previewkeydown event..
Check if txtAmount is focused in the Form_KeyDown:
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
'//Escape keypress
MsgBox("TEXTBOX ESCAPE")
sender.Text = ""
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If txtAmount.Focused Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
Edit: Alternatively, if you don't want to specify each control individually, you could do it by type:
If TypeOf Me.ActiveControl Is MaskTextBox Then Return
Or even more simpler, change your Form_KeyDown-Event to a KeyUp Event.
Public Class frmMainMenu
Private KeyHandled As Boolean
Private Sub txtAmount_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtAmount.KeyDown
If e.KeyCode = Keys.Escape Then
sender.Text = ""
KeyHandled = True
End If
End Sub
Private Sub frmMainMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
KeyHandled = False
End Sub
Private Sub frmMainMenu_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If KeyHandled Then Return
If e.KeyCode = Keys.Escape Then Application.Exit()
End Sub
End Class
Here is my code
Private Sub When_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown, MyBase.KeyDown
If sender.name = "Your form name" Then
If e.KeyCode = Keys.Escape And Not TextBox1.Focused Then
MsgBox("me")
End If
ElseIf sender.name = "your textBox name" Then
If e.KeyCode = Keys.Escape Then
MsgBox("txt")
End If
End If
End Sub
I hope this can help you

Keypress in not read in visual basic 2010/2012

My app is based on Visual Basic 2010/2012 (all its codes are same for both the languages). I'm trying to do certain things when either the 'F' or 'G' is pressed from keyboard. I've tried all the things like using Keypress, keydown and keyup events but they all didn't work.
Here is a sample code of my app. This just shows which key was pressed by the user in a message box and this also doesn't work i.e. nothing happens, not even an error message.
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.F)) Then
MessageBox.Show("pressed F")
ElseIf (e.KeyCode = Keys.Control AndAlso (e.KeyCode = Keys.B)) Then
MessageBox.Show("pressed B")
End If
End Sub`
My take on optimizing the KeyDown handler, using DRY principle (=don't repeat yourself):
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If Not e.Control Then Exit Sub
Select Case e.KeyCode
Case Keys.F : MessageBox.Show("Ctrl-F")
Case Keys.B : MessageBox.Show("Ctrl-B")
End Select
End Sub
In keypressed
Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
dim kc as string = e.KeyChar
if kc = "F" OR kc = "G" then msgbox "Horeeee"
End Sub
In addition to setting KeyPreview() to True for your Form, as already pointed out by #Tony Hopkinson, here is how to correctly check for Ctrl-F or Ctrl-B:
Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control AndAlso e.KeyCode = Keys.F Then
MessageBox.Show("Ctrl-F")
ElseIf e.Control AndAlso e.KeyCode = Keys.B Then
MessageBox.Show("Ctrl-B")
End If
End Sub

vb.net key combination

I'm trying to capture two key presses in my VB.net application, for this example CTRL + B, the code below doesn't work but it does for single keys. I have tried setting keypreview as true but this has no effect.
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.B Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
End Class
Thanks
The Control key is a Modifier key. This code tests for Ctrl + B
e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control
The key-code is B, but the modifier is Ctrl.
Your code snippet, updated:
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If (e.KeyCode = Keys.B AndAlso e.Modifiers = Keys.Control) Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
You need to add controlkey also after modifier to make it work properly.
Private Sub main_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles mybase.KeyDown
If (e.KeyCode = Keys.B AndAlso e.Modifiers.ControlKey = Keys.Control) Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
If you look at the documentation for KeyEventArgs, you'll note that the class exposes properties for ALT, CTRL and Modifiers which allow you to determine whether these keys were pressed in addition to the main symbol key you're interested in.
Private Sub main_KeyDown(
ByVal sender As Object,
ByVal e As System.Windows.Forms.KeyEventArgs)
Handles mybase.KeyDown
If e.Control And e.KeyCode = Keys.B Then
MsgBox("CTRL + B Pressed !")
End If
End Sub
You can use this:
If e.Control = True And e.KeyCode = Keys.B Then
'Do Something...
End If