© copyright 09.Oct.2009 by Paul Bradley filed under Visual Basic
I have been programming with Visual Basic for over 7 years, and have never needed to add function key actions to command buttons until this week. All the applications I have developed in the past have been mouse orientated, and have not required keyboard shortcuts for all the major functions.
However this new application I am developing is going to be used by power users, who require consistent keyboard shortcuts across all data entry screens, so I had to come up with keyboard function key shortcuts for the applications major functions.
After using the DOS database engine DataEase for many years, I decided to borrow their function key mappings which where F2 to insert a new record, F7 to delete an existing record and F8 to update the current record. Below are the steps I took to achieve this, each form has three command buttons to perform each of these tasks.
Firstly you need to add some text to each of the command buttons, to give your users visual feedback as to the shortcut being used for that button. I prefer to put these keyboard mappings in square brackets.
Next you need to go to the properties window for the form, and find the KeyPreview property and change it True. This will allow you to monitor each key being pressed while the form is active, filtering your desired function key shortcuts for special attention.
Private Sub frmDemographics_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.F2
cmdInsert.PerformClick()
Case Keys.F7
cmdDelete.PerformClick()
Case Keys.F8
cmdUpdate.PerformClick()
End Select
End Sub
About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He has over 20 years programming experience.
He also produces e-learning videos at Linux by Example