How to assign a shortcut key (something like Ctrl+F) to a text box in Windows Forms?
One way is to override the ProcessCMDKey event.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.S))
{
MessageBox.Show("Do Something");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
EDIT: Alternatively you can use the keydown event - see How to capture shortcut keys in Visual Studio .NET.
Capture the KeyDown
event and place an if statement in it to check what keys were pressed.
private void form_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Control && e.KeyCode == Keys.F) || (e.Control && e.KeyCode == Keys.S)) {
txtSearch.Focus();
}
}