Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

In the text of the button put an & in front of the letter you want to use for the hot key.

superButton.Text = "&Save" 'You’d probably set this through the designer

Will look like Save and hit with hot keys using Alt+S.

Edited by mskeel
  • *Experts*
Posted

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.

   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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
  • Leaders
Posted
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.
[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...