hotkey to button in C# Windows application

c#

Solution

Override Form's `ProcessCmdKey`. If you find a keystroke that you like, call the same method that the button would.

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!");
}

EDIT: Jay's method is better if you can find an appropriate mnemonic.

Problem

How do i make a hotkey to button in C# Windows application

Original source