0

i have buttons and i would liek to make shortcuts from the keyboard to press them.

eg. btnAdd = CTRL+A

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Momerath
0

Set the button's UseMnemonic property to true and add an ampersand (&) just before the letter in the button's Text property you want to use for the hotkey. This will automatically assign the ALT-<key> to that button.

If you want to use CTRL or SHIFT you'll have to override the forms ProccessCmdKey method to look for the keys you want and then call the same method there that you would in your button handler, something like this:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
    if (keyData == (Keys.Control | Keys.X)) {
        DoSomething();
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

private void button1_Click(object sender, EventArgs e) {
    DoSomething();
}

private void DoSomething() {
    MessageBox.Show("hi!");
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.