How to use short keys against button?

In the text of the button put an & in front of the letter you want to use for the hot key.
Visual Basic:
superButton.Text = "&Save" 'You’d probably set this through the designer
Will look like Save and hit with hot keys using Alt+S.
 
Last edited:
Ctrl-S is a bit harder. I believe there are really only two options:
1. If you have a menu, you can set the menu item's Shortcut property to CtrlS.
2. If you don't have a menu, you'll have to trap this yourself. Generally, you'll set the form's KeyPreview property to True - this lets the form get all keyboard events before any controls. Then, in the KeyDown event, check to see what the user was pressing.
Visual Basic:
    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.Control And e.KeyCode = Keys.S Then
            MessageBox.Show("wow")
        End If
    End Sub

If you want this code to click a button (not recommended), you can use Button1.PerformClick(). If your code is behind a button, it's best to move it into a function (use Refactor->Extract Method, it's fun!). Then you would call that function from your button and in the KeyDown event.

-ner
 
If you want a quick and dirty hack, you can use a MenuStrip, create a ToolStripMenuItem for each shortcut key, attach a handler, and then set the MenuStrip's visible property to false. Like I said, it is a quick and dirty hack. I would never use it, but its there.
 
Back
Top