Shortcuts in .Net, Winforms, C#

From Shortcuts Wiki
Jump to: navigation, search

Here is how to create some shortcuts in .Net, Winforms, C#.

Create a WinForm[edit]

Create a new WinForm (with Visual Studio : menu>project>Add new item...>Winform).

In the properties editor, you don't need to set its KeyPreview property to true.

Override the ProcessCmdKey method[edit]

In the winform code, override (redefine) the ProcessCmdKey method. Inside, test the second parameter of type Keys to know which keys were pressed. Return true if processed, if not, call the parent method.

Example :

 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
   if (keyData == (Keys.Control | Keys.C)) {
     //do copy something
     return true;
   return base.ProcessCmdKey(ref msg, keyData);

More informations[edit]