Capture keys.TAB on KeyDown - vb.net

I am trying to capture TAB keypress on Keydown Event.
I can see another post on How to fire an event when the tab key is pressed in a textbox?
However, On the above link, posted solution is not working for me which I mentioned below.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
'do something
End If
End Sub
For the testing purpose, I have added 2 simple textboxes on FORM1 and write the below code to capture the TAB on KeyDown event.
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
e.SuppressKeyPress = True
MsgBox("TAB DOWN")
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Me.Text = e.KeyChar
End Sub
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Tab Then
MsgBox("TAB UP")
End If
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Me.Text = "LEAVE"
End Sub
My above code should suppose to display a message box on KeyDown when TAB is press. It's not working.
Please let me know what I am doing wrong.
Thanks in advance!!!

I found a new event called PreviewKeyDown()
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Tab Then
Me.Text = "TAB Capture From TextBox1_KeyDown At " & Now.ToString
End If
End Sub
Private Sub TextBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown
If e.KeyCode = Keys.Tab Then
Me.Text = "TAB Capture From TextBox1_PreviewKeyDown At " & Now.ToString
End If
End Sub
If you will execute the above code, you will able to capture TAB key on PreviewKeyDown() event.

MsgBox() is a holdover from VB6 and you should use the .NET implementation of a message box, like this:
MessageBox.Show("TAB UP")
Also, you are setting a Text property against the instance of the form class (Me), when I think you intend to set the Text property of the text box, like this:
Me.TextBox1.Text = e.KeyChar

Related

Key Press to Close Form VB.net

So I've been working on this project to autorun on my flash drive, whenever it gets plugged it. It just displays my contact info and a little message, so I can get it returned.
Because I want people to actually read the message, the close button is disabled. You have to check a little box, and then hit the "OK" button.
Of course, I don't want to do this whenever I plug in my own flash drive. I'm trying to make a keyboard shortcut to close it automatically. I saw this post here but it didn't work for me. Here's the code from that post:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If
End Sub
Any help is very appreciated. Thanks!
Also, if you think my code is sloppy and want to clean it up, here it is.
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
End
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
ButtonOK.Enabled = True
Else
ButtonOK.Enabled = False
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
End
End If
End Sub
End Class
Firstly, NEVER EVER use End. That is just plain bad under any circumstances.
What you should be doing is testing the Checked value of your CheckBox in the FormClosing event handler and cancelling if and only if it's not checked. Logically, you should then check the CheckBox and call Close on the form when you detect that desired key combination.
Actually, I'd probably not call Close either, but rather call PerformClick on the Button. That way, the key combination is guaranteed to do exactly the same thing as clicking the Button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
Close()
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
ButtonOK.Enabled = CheckBox1.Checked
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
CheckBox1.Checked = True
ButtonOK.PerformClick()
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = Not CheckBox1.Checked
End Sub

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

VB.NET - Check if the key pressed is the same as the text in label

I am wondering if anyone can help me to do the above question.
Basically what i have is a label which has had a randomly generated letter placed into it.
What I want to do is to show a msgbox (just to show that it works for now) when the same key in the label is pressed. I have tried two methods but none seem to work, could anyone point me in the right direction? It doesn't seem like it would be that hard, i am just new to coding.
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar = lblletter1.Text.ToString) Then
MsgBox("WORKS")
ElseIf (e.KeyChar = lblletter2.Text) Then
MsgBox("words")
End If
Many thanks!
In order to handle the form's KeyPress event, you need to set it's KeyPreview property. You can do that in the Form Designer or in the form's Load event handler as below.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = lblletter1.Text(0) Then
MessageBox.Show("WORKS")
ElseIf e.KeyChar = lblletter2.Text(0) Then
MessageBox.Show("words")
End If
End Sub
End Class
Try this
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.ToString = lblletter1.Text) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.ToString = lblletter2.Text) Then
MsgBox("words")
End If
End Sub
Try this
Private Sub speedtyping_Keydown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (e.KeyChar.Trim() = lblletter1.Text.Trim()) Then
MsgBox("WORKS")
ElseIf (e.KeyChar.Trim() = lblletter2.Text.Trim()) Then
MsgBox("words")
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

Howto check if control key is released

I am searching how to detect if control key, and no other keys, is released (in textbox).
This is what I came up so far:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = 17 Then
GetTick = System.Environment.TickCount
End If
End Sub
This work's but triggers also in combination keys like Ctrl+c etc...
With what to replace 17?
Have you tried e.Control, as in the following:
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
If e.Control Then
GetTick = System.Environment.TickCount
End If
End Sub
Adapted from:
http://www.devcurry.com/2009/10/detecting-ctrl-and-shift-keys-on-form.html
I haven't used VB in a while but here's some stuff that might help you.
You can try creating a boolean variable (true false), called ctrl or something like that. Make it true when the control key is first pressed, then make it false when they put the key up. Then you could create another variable that stores all the key codes called keycodes or something. Make keycodes -1 when a key other than control is pressed. Here is some example code :
Dim kys=-1
Dim ctrl=false
Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
if e.KeyValue = Keys.Control then
ctrl=false
else
kys=-1
end if
End Sub
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
if e.KeyValue = Keys.Control then
ctrl=true
else
kys=e.getKeyValue
end if
'Control c
if ctrl=true & kys=Keys.c then
MsgBox("It Worked!")
end if
End Sub
By the way, e.KeyValue is usually better than e.keyCode for stuff like this, it's easier to use because you can just use "Keys" instead of looking up all the keycodes.