|
-
Jul 8th, 2009, 04:20 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Shortcut keys do not work
I have formA.vb and I added some shortcut keys using the key_up event handler. Before coding the handler I change the keypreview property of the form to true. I added this code :
Code:
Private Sub IntRMA_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.C Then
modcustomer.Show()
ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.T Then
Me.Button6.PerformClick()
ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.I Then
Me.Button5.PerformClick()
ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.S Then
Me.Button1.PerformClick()
ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.P Then
Me.Button9.PerformClick()
ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.X Then
Me.Close()
ElseIf e.KeyCode = Keys.Escape Then
Me.Close()
End If
End Sub
..... Only the last option of the Escape works. Thanks for the time and replies. Waiting for the sun between the rain
-
Jul 8th, 2009, 04:29 AM
#2
Re: Shortcut keys do not work
Try this :
Code:
Private Sub IntRMA_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
If e.Control = True And e.KeyCode = Keys.C Then
modcustomer.Show()
ElseIf e.Control = True And e.KeyCode = Keys.T Then
Me.Button6.PerformClick()
ElseIf e.Control = True And e.KeyCode = Keys.I Then
Me.Button5.PerformClick()
ElseIf e.Control = True And e.KeyCode = Keys.S Then
Me.Button1.PerformClick()
ElseIf e.Control = True And e.KeyCode = Keys.P Then
Me.Button9.PerformClick()
ElseIf e.Control = True And e.KeyCode = Keys.X Then
Me.Close()
ElseIf e.KeyCode = Keys.Escape Then
Me.Close()
End If
End Sub
-
Jul 8th, 2009, 04:37 AM
#3
Re: Shortcut keys do not work
That's because you're testing whether e.KeyCode is two different values at the same time, which it can't possibly be. If you don't care about modifier keys then just test e.KeyCode for a single key. If you do care about modifier keys then test e.KeyData for a key combination. Your code should be:
vb.net Code:
Private Sub IntRMA_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp Select Case e.KeyData Case Keys.Control Or Keys.C modcustomer.Show() Case Keys.Control Or Keys.T Me.Button6.PerformClick() Case Keys.Control Or Keys.I Me.Button5.PerformClick() Case Keys.Control Or Keys.S Me.Button1.PerformClick() Case Keys.Control Or Keys.P Me.Button9.PerformClick() Case Keys.Control Or Keys.X Me.Close() Case Keys.Escape Me.Close() End Select End Sub
-
Jul 8th, 2009, 04:55 AM
#4
Thread Starter
Addicted Member
Re: Shortcut keys do not work
thanks jmcilhinney. that did the work. great
-
Jul 8th, 2009, 10:28 PM
#5
Frenzied Member
Re: [RESOLVED] Shortcut keys do not work
I am a beginner of .net framework and the visual basic language
I added the following code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Control = True And e.KeyCode = Keys.P Then
Me.Button1.PerformClick()
End If
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("This is a message box")
End Sub
but this is not working for me.....
can you tell me why its not working?
-
Jul 9th, 2009, 04:50 AM
#6
Thread Starter
Addicted Member
Re: [RESOLVED] Shortcut keys do not work
What exactly are trying to do? Get the button1 to perform click? What keys you want to use to enable this shortcut? the control button and P? If, yes the try this:
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyData = Keys.Control And e.KeyData = Keys.P Then
Me.Button1.PerformClick()
End If
End Sub
-
Jul 9th, 2009, 05:19 AM
#7
Re: [RESOLVED] Shortcut keys do not work
 Originally Posted by gate7cy
What exactly are trying to do? Get the button1 to perform click? What keys you want to use to enable this shortcut? the control button and P? If, yes the try this:
Code:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyData = Keys.Control And e.KeyData = Keys.P Then
Me.Button1.PerformClick()
End If
End Sub
How can e.KeyData be equal to Keys.Control and equal to Keys.P at the same time? As has been established by post #3, you have to compare e.KeyData to (Keys.Control Or Keys.P), but this has already been explained to gautamshaw in their own thread on this topic, where they asked this exact same question.
-
Jul 9th, 2009, 06:20 AM
#8
Thread Starter
Addicted Member
Re: [RESOLVED] Shortcut keys do not work
my bad
drop the 'and' and replace it with an 'or'.
how on earth would I know of the anwer he got on his own thread.
-
Jul 9th, 2009, 07:45 AM
#9
Re: [RESOLVED] Shortcut keys do not work
 Originally Posted by gate7cy
drop the 'and' and replace it with an 'or'.
That still won't work because you're then testing for one value or the other, not both at the same time. You have to compare e.KeyData to a single value and that value has to be a combination of Keys.Control and Keys.P, which you create with a bitwise OR operation.
 Originally Posted by gate7cy
how on earth would I know of the anwer he got on his own thread.
You wouldn't, which is exactly why people shouldn't be posting the same question in mutliple threads. That was directed at gautamshaw, not you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|