Setting short cut keys for menus, buttons etc are very easy in the .net framework, you just have to set the ShortCut property for menu items. And for buttons etc, you can indicate the access key to be used for the control by putting a & sign just before the letter which should be used as the access key like, setting the text of the button control to "&Open" would set the shortcut key "Alt+O" for the button. However, if you want use other key combinations for the shortcut keys like Ctrl+a, Ctrl+F10 etc, for any controls other than menus or if you want to provide shortcut keys for textbox, you can't simply set any property or use the & sign to indicate it.

Let us solve the first issue

Giving focus to textboxs when the short cut key is pressed.
To be able to do this, you should add a label to the form and set it's UseMnemonic property to true. Now set the text property of that label. Suppose, the label is for a name field. Now set the Text property to "&Name". Now set the TabIndex of the name TextBox to 1 more than that of the Label Control. Now users can use "Alt+N" to activate the textbox. When users presses this short cut key, The focus is provided to the next control in the tab order after the Label control.

Now to the main point of the post

setting virtually any key combination as the shortcut key: Only way in which this can be done is by writing some code to check the key combinations and take the necessary action.
I use the form's KeyDown event for this process. Also you must set the KeyPreview property of the Form to True. So that the Form receives all the key events before it gets passed to the active control. Also it is the only way by which you can have keyboard shortcuts like Alt+1, Alt+2 so on for giving focus to specific controls etc (the method described above will not work as you will have to include the letter which should be the access key in the Label's Text, which may not possible always).

Sample code

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.Alt Then
Dim t As TextBox = Nothing
Select Case e.KeyCode
Case Keys.D1
t = txtName
Case Keys.D2
t = txtDescription
Case Else
Return
End Select
t.Focus()
t.Select(0, t.Text.Length)
e.SuppressKeyPress=True
End If
End Sub

The code above sets focus to 2 Textboxs depending up on the key pressed with the Alt key. Code first make sures that Alt key is pressed. Then, it does a select case on the KeyCode property of the event argument. If the key is 1 or 2, it sets the local variable t to the respective TextBox. In the Case Else, the code just returns so that the statements after End Select does not gets executed if the user press any other key with the Alt key. The statements after the End Select just sets the focus to the TextBox and sets the SuppressKeyPress property of the event argument to True. So that the menus does not get activated because the Alt key is pressed. Also, this insures the active control does not get this key event, avoiding any possible trouble for the user.

These are the ways which I found to enable short cut keys in your .net applications. I will post updates if I get any new method/tricks.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList