|
-
Nov 12th, 2007, 01:31 PM
#1
Thread Starter
Member
Keydown Question
I have a form with two buttons. I would like to add keyboard shortcuts for those buttons and have looked through all of the postings I could find on this topic. I did see one post which had the code below.
It works for the first time that the user presses F2 but only that one time. Can you please help out a newbie?
vb Code:
Private Sub PB_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.F2 Then
MsgBox("pushed F2")
End If
End Sub
Last edited by dtgreer; Nov 12th, 2007 at 03:17 PM.
-
Nov 12th, 2007, 01:34 PM
#2
Re: Keydown Question
Try
vb.net Code:
If Asc(e.KeyChar) = Keys.F2 Then
-
Nov 12th, 2007, 01:38 PM
#3
Re: Keydown Question
Try setting the KeyPreview property of the Form to True.
That should sort it out.
-
Nov 12th, 2007, 01:42 PM
#4
Re: Keydown Question
He said it worked one time, but not twice. If KeyPreview was not set to true, it wouldn't work at all, ever, right?
-
Nov 12th, 2007, 02:11 PM
#5
Thread Starter
Member
Re: Keydown Question
Ok I am getting this error.
'KeyChar' is not a member of 'System.Windows.Forms.KeyEventArgs'.
Is there something I need to import?
-
Nov 12th, 2007, 02:17 PM
#6
Re: Keydown Question
Is PB a Form or a control?
-
Nov 12th, 2007, 02:17 PM
#7
Thread Starter
Member
-
Nov 12th, 2007, 02:19 PM
#8
Re: Keydown Question
 Originally Posted by dtgreer
Sorry it is a form
Ohhhh....then
 Originally Posted by dtgreer
'KeyChar' is not a member of 'System.Windows.Forms.KeyEventArgs'.
is correct, and no you didn't miss anything.
When I run your original code from the Form's KeyDown event, it works for me each and everytime.
-
Nov 12th, 2007, 02:48 PM
#9
Thread Starter
Member
Re: Keydown Question
well thank you for your help. It is working perfectly now.
-
Nov 12th, 2007, 02:52 PM
#10
Re: Keydown Question
Did you do or change anything?
-
Nov 12th, 2007, 03:16 PM
#11
Thread Starter
Member
Re: Keydown Question
the only change I made was set the Keypreview to true.. It is odd that it would work at all with it set to false.
I have tested it again and it appears to work the same whether I set the Keypreview attribute of the form to true or not. The reason it would only work one time before is the msgbox pop-up.
Whenever the msgbox will open, I guess the form loses focus even if I close the msgbox.
I am sure you can explain why that happens and how to fix that problem.
-
Nov 12th, 2007, 03:20 PM
#12
Thread Starter
Member
Re: Keydown Question
Correction.
Scenario 1: Keypreview set to false. Works but if a msgbox or anything else taking focus pop-ups then it will no longer work.
Scenario 2: Keypreview set to true. Works correctly regardless of msgboxes etc.
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
|