Best way to implement keyboard shortcuts in a Windows Forms application?


I'm looking for a best way to implement common Windows keyboard shortcuts (for example Ctrl+F, Ctrl+N) in my Windows Forms application in C#.


You probably forget to set the form's KeyPreview property to True. Overriding the ProcessCmdKey() method is the generic solution:


protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.F)) {
MessageBox.Show("What the Ctrl+F?");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

0 comments:

Post a Comment