Windows form hotkeys - vb.net

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

Related

VB disable "|" key

I'm trying to disable the | key in a textbox as my csv file uses the character as a separator. However, I tried many methods and searched on google but I can't find any solutions related to that.
My code:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.KeyCode = Keys.Shift And e.KeyCode = AscW(220) Then
e.SuppressKeyPress = True
End If
End Sub
Wonder if that is possible? Thanks
If you want to detect a key in combination with one or more modifiers then there are multiple ways to do it. You could do this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If Not e.Control AndAlso e.Shift AndAlso Not e.Alt AndAlso e.KeyCode = Keys.Oem5 Then
e.SuppressKeyPress = True
End If
End Sub
or this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.Modifiers = Keys.Shift AndAlso e.KeyCode = Keys.Oem5 Then
e.SuppressKeyPress = True
End If
End Sub
but my preference would be this:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
If e.KeyData = (Keys.Shift Or Keys.Oem5) Then
e.SuppressKeyPress = True
End If
End Sub
Note that that third snippet uses KeyData rather than KeyCode. The latter is just the current key while the former is the current key and any modifier keys.
If you want to make it a little more succinct:
Private Sub txtRemarks_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRemarks.KeyDown
e.SuppressKeyPress = (e.KeyData = (Keys.Shift Or Keys.Oem5))
End Sub

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

How to suppress sound when opening a dialogform in vb.net

I have a form with a textbox, when pressing enter another form2 is opened.
How can I suppress sound when form2 is opened?
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
e.SuppressKeyPress = True
My.Forms.Form2.ShowDialog()
End If
End Sub
You could do this by changing the code slightly and moving it into the KeyPress event instead of KeyDown
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyKeyPress
If e.KeyChar = ChrW(Keys.Enter) Then
e.Handled = True
My.Forms.Form2.ShowDialog()
End If
End Sub

Eat key in KeyDown

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.V Then
If e.Control = True Then
e.Handled = True 'eat it
End If
End If
End Sub
This does not work.
Does anybody know why and how to achieve the same in a proper way?
Thank you!
ps: Why is there a .Handled property that is writable, but it does not do anything? I guess I missed something.
You should capture the KeyPress event instead of KeyDown:
Private isCopyPaste As Boolean = False
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
isCopyPaste = (e.KeyCode = Keys.V AndAlso e.Control)
End Sub
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs ) Handles TextBox1.KeyPress
If isCopyPaste Then
e.Handled = True 'eat it
End If
End Sub
Also check this reference on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
Use the SuppressKeyPress instead:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.V Then
If e.Control = True Then
e.SuppressKeyPress = True
End If
End If
End Sub

How to handle KeyDown in 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