Keydown event weird behaviour - vb.net

I'm trying to catch shortcut keys. I need an explanation on how KeyDown Events are managed. Let's take this as an example :
Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
If e.Control And e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
End If
End Sub
Works perfectly. No matter which one I press first or how many times I press them both MessageBox will pop up. Now if I simply change the order inside the sub :
Private Sub SoldeOuvertFou_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control And e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
End If
If e.Control And e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
End Sub
With this approach, only Ctrl+F5 will pop-up. No way to make Ctrl+W appear... any idea why ?

you can simply use :
If e.Control Then
If e.KeyCode = Keys.F5 Then
MessageBox.Show("Ctrl+F5")
Else
If e.KeyCode = Keys.W Then
MessageBox.Show("Ctrl+W")
End If
End If
End If

Related

How to make keypress (keydown) close program

I'm currently writing code for a project in VB.NET where if you hit the "escape" button you get a messagebox saying "This exits the program, would you like to leave? 'Y' or 'N'". What code would I write to make it so that if you hit "Y" the program closes?
This is what I have so far:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
MsgBox("This button exits the program. Do you want to exit the program?")
If Button = e.KeyCode = Keys.Y Then
End
End If
End If
End Sub
This will give you what you are after:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
If MessageBox.Show("message", "caption", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Close()
End If
End If
End Sub
Screenshot showing how it looks when I press the Esc button:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.Escape
Msgbox("Close",vbInformation)
Me.Close()
End Select
End Sub
Hope This Helps as a guide.
Try This One
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Escape Then
Dim ask As MsgBoxResult
ask = MsgBox("Close Program ?", MsgBoxStyle.YesNo, "Exit")
If ask = MsgBoxResult.Yes Then
Me.Close()
End If
End If
End Sub

textbox.focus does not work. vb

i have a problem with command textbox.focus it does not work "when I entered the key it goes to under line inside the textbox so i can not make another command because there is a one space or the navigation arrow still in the second row inside the textbox"...
this is my code
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox1.Clear()
TextBox1.Focus()
End If
End Sub
when i use this code
If e.KeyCode = Keys.Enter Then
msgbox("any thing")
TextBox1.Clear()
TextBox1.Focus()
End If
it works good
any one help me please?
thanks
I have got the answer
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox1.Text = ""
SendKeys.Send("{BACKSPACE}")
TextBox1.Focus()
End If
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