Wed 21 Jan 2009

Tonight in windows form, using C# language I required to use key combination like we use Ctrl+S to save form, CTRL+F to find etc.

In windows form, its quite easy. Just enable KeyPreview property to true. This property makes the form get the key events before the controls, so you can set the KeyPress event on the form.

Form1.KeyPreview = true;

After that, set the form event for the keypress/key down

this.KeyDown += new KeyEventHandler(this.Form1_KeyDown);

Now shortcut part comes here, follow like this

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
 if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.S)
 {  
  //SaveData();
 }
 else if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.F)
 {  
  //Find();
 }
}

Hope it helps some one.

 

Tags: ,
E-mail this post to someone or Comments (1)

Comments

5/26/2009 | Zaki  Russia
.KeyPreview = true; //requred thing

Thank you very much!

Add Comment Post comment

 
 
 
   Country flag

Loading

For mobile, tablets or touch devices, if verify button is disabed, please press Enter after entering captcha verfication text than click Verify button.