Results 1 to 12 of 12

Thread: Keydown Question

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    63

    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:
    1. Private Sub PB_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    2.         If e.KeyCode = Keys.F2 Then
    3.             MsgBox("pushed F2")
    4.         End If
    5.     End Sub
    Last edited by dtgreer; Nov 12th, 2007 at 03:17 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Keydown Question

    Try
    vb.net Code:
    1. If Asc(e.KeyChar) = Keys.F2 Then

  3. #3
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: Keydown Question

    Try setting the KeyPreview property of the Form to True.

    That should sort it out.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    63

    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?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Keydown Question

    Is PB a Form or a control?

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    63

    Re: Keydown Question

    Sorry it is a form

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Keydown Question

    Quote Originally Posted by dtgreer
    Sorry it is a form
    Ohhhh....then
    Quote 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.

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    63

    Re: Keydown Question

    well thank you for your help. It is working perfectly now.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Keydown Question

    Did you do or change anything?

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    63

    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.

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2007
    Posts
    63

    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
  •  



Click Here to Expand Forum to Full Width