I want to create a Shift+F5 key press event in VB.net. I used this code for creating Shift+F5 event in vb but it is not working.
Private Sub IBR_SJK_P110_000_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.Modifiers
Case Keys.Shift
If e.KeyCode = Keys.F5 Then
MsgBox("Shift F5 Pressed")
End If
End Select
End Sub
But if i used
If e.KeyCode = Keys.A Then
than It is working well.
Please Help me.
This is an example :
If e.KeyData = (Keys.F5 and Keys.Shift) Then
MessageBox.Show("got here")
End If
Another thing you can attempt is this:
If e.Shift = True AndAlso e.KeyCode = Keys.F5 Then
MsgBox("Shift F5 Pressed")
End If
The e.Modifiers property seems to be bugged on some computers/with some keyboards...
Related
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
Now I want to get the new value what I put into my datagrid; however, I always get the old value if I keys Up or Down.
Here is my KeyDownEvent Function:
Private Sub grd_MyGridKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grd.MyGridKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
Me.grd.Item(Me.grd.CurrentRowIndex, 4) = Me.grd.Item(Me.grd.CurrentRowIndex, 2) * Me.grd.Item(Me.grd.CurrentRowIndex, 3)
End If
End Sub
On your datagridview CellValidating Event
e.Cancel = False
I just found the solution in Japanese website, so I just post what I used to solve my problem.
Me.grd.Item(Me.grd.CurrentRowIndex, 3) = DirectCast(ColumnMoney, DataGridTextBoxColumn).TextBox.Text
grd is my datagrid, and this is the solution. Hope that this will help others who also meet this problem.
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
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
I am trying to detect the keys "Control" and "t" being pressed simultaneously in VB.NET. The code I have so far is as follows:
Private Sub frmTimingP2P_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.ControlKey And e.KeyValue = Keys.T Then
MessageBox.Show("Ctrl + T")
End If
End Sub
I can detect one key or the other by removing the and statement and the second keyvalue statement, but I don't really get anything when I try this. Is there another method?
Thanks
First of all, And in your code should be AndAlso since it’s a logical operator. And in VB is a bit operator. Next, you can use the Modifiers property to test for modifier keys:
If (e.KeyCode And Not Keys.Modifiers) = Keys.T AndAlso e.Modifiers = Keys.Control Then
MessageBox.Show("Ctrl + T")
End If
The e.KeyCode And Not Keys.Modifiers in the first part of the condition is necessary to mask out the modifier key.
If e.Modifiers = Keys.Ctrl can also be written as If e.Control.
Alternatively, we can collate these two queries by asking directly whether the combination Ctrl+T was pressed:
If e.KeyCode = (Keys.T Or Keys.Ctrl) Then …
In both snippets we make use of bit masks.
Private Sub frmMain_Zaporka_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyData
Case (Keys.Control + Keys.Shift + Keys.F12)
MsgBox("Control + Shift + F12")
Case (Keys.Escape)
Me.Close()
End Select
' or
If e.KeyCode = Keys.F12 AndAlso e.Modifiers = (Keys.Control Or Keys.Shift) Then
MsgBox("Control + Shift + F12")
ElseIf e.KeyCode = Keys.Escape Then
Me.Close()
End If
' or
Select Case e.KeyCode
Case (Keys.F12 And e.Control And e.Shift)
MsgBox("Control + Shift + F12")
Case (Keys.Escape)
Me.Close()
End Select
End Sub
I had the same problem, but for me to get this to work I had to set the forms KeyPreview property to true. In Visual studio you can change this in the Forms [Design] Property window or changing the property on load.
Private Sub frmTimingP2P_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub
then use by using:
Private Sub frmTimingP2P_KeyDown(ByVal Sender As Object, ByVal e As _
System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If (e.KeyCode = Keys.T AndAlso e.Modifiers = Keys.Control) Then
MessageBox.Show("Ctrl + T")
End If
End Sub
or other program logic as provided in the answers above.
I'll save you from the long code.
Here:
If e.Control And e.Alt And e.KeyCode = Keys.G Then
MsgBox("Control Alt G")
End If
I dont have vb.net installed right now but try this on your keydown or keypress event:
If e.KeyCode = Keys.T AndAlso e.Control = True Then
MsgBox("Ctrl + T")
End If
I actually found through experimentation that the KeyPreview setting is irrelevant when code is processed through the "KeyDown" or "KeyUp" routines we add into our code. Perhaps the automatic inbuilt code for keypress takes the KeyPreview setting into account, but ours does not even have to consider it.
I found the best approach is the one shown in KuroMoro's answer, using e.KeyData plus various "Case Statements".
The following works beautifully to insert symbols into a textbox when certain keys are pressing with the Control key simultaneously.
Private Sub Comments_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Comment.KeyDown, Response.KeyDown
Select Case e.KeyData
Case (Keys.S + Keys.Control)
SendKeys.Send("♠")
Case (Keys.H + Keys.Control)
SendKeys.Send("♥")
Case (Keys.D + Keys.Control)
SendKeys.Send("♦")
Case (Keys.C + Keys.Control)
SendKeys.Send("♣")
End Select
End Subcode
Like Chris Raisin's approach, I use the KeyUp event. Otherwise, pressing the Control key can trigger an action before you press a second key. Here's an example of my code to test for CTRL-A in a listview control named lvSpectra:
Private Sub lvwSpectra_KeyUp(sender As Object, e As KeyEventArgs) Handles lvwSpectra.KeyUp
If e.Control And e.KeyCode = Keys.A Then
' Do something
End If
End Sub