Pages

Saturday, June 02, 2007

Using Function Keys in Visual Basic Forms

Function keys as Shortcut Keys in VB
Function keys are a boon for assigning shortcuts. They have more advantage than the regular Alt + or Ctrl + combination.

Function keys can be assigned to command buttons using simple tricks as explained below. For that we need to instuct the VB to handle them in t
he Keydown event by setting the Keypreview = True




Then you can have the necessary shortcut keys on the form



In the Form_KeyDown event redirect to necessary functions/procs based on the key

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case vbKeyF2

Call Proc_Fund_Transfer

Case vbKeyF3

Call Proc_Credit_Card


End Select

End Sub


6 comments:

  1. Anonymous8:04 AM

    I wanted to use function keys to move through a recordset displayed in a form. ANd the keydown event works with the keypreview property of the form set to true. HOWEVER, I used F10 and F9 as movenext and moveprevious
    and when I look at the keycodes being generated , on of the funciton keys only regristers on EVERY-OTHER keydown event, yet the KEYUP event fires correctly. DOes anyone know why this would be or how to fix it.

    Thanks!

    ReplyDelete
  2. Anonymous3:42 PM

    I need to do this but in visual studio 2008, anyone can help me

    ReplyDelete
  3. Check if the following helps you:

    http://dotnetdud.blogspot.com/2011/05/how-to-use-function-keys-in-vbnet.html

    ReplyDelete
  4. This one doesn't work for me. I've tried to use KeyDown event of my form. But the Sub is never called.

    ReplyDelete
  5. Anonymous4:05 AM

    Does work OK for me.

    ReplyDelete
  6. Anonymous7:08 AM

    thanks >>> much thanks

    ReplyDelete