Tech Off Thread

6 posts

Shortcut keys to buttons in C# program

Back to Forum: Tech Off
  • User profile image
    GurliGebis

    Hey.

    I know it's possible to have shortcut keys to menu items in a C# program (Like CTRL+F5).
    Is it possible to have shortcut keys for Buttons, or do I need to have a menu just to get the shortcut keys?

  • User profile image
    TadejK

    You could set the KeyPreview property of your Form to true, and then subscribe to the KeyDown event.
    There, you simply put an if statement .. if (e.keycode == keys.return) ...

    Regards,
    Tadej

  • User profile image
    GurliGebis

    Thanks alot, I'll try that Smiley

  • User profile image
    SimonJ

    Buttons normally have accelerator keys (EG Alt+G) rather than shortcut keys (EG Ctrl+G). Accelerator keys are set using the Text property of the button. Put an ampersand (&) before the letter in the caption you want the button to react to.

    When the user presses Alt, all the accelerators on the form get underlined.

    Note that the form's Accept button automatically reacts to the Enter key and it's cancel button reacts to the Escape key. Both of these are properties of the form.

    You should not put Accelerators on the Accept or Cancel button.

    IE Button captions should not be OK or Cancel because you don't need to be able to press Alt+O as well as Return to do the same thing.

    SimonJ

  • User profile image
    Drago

    Thanks a lot. It works.

  • User profile image
    Deactivated User

    Comment removed at user's request.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.