I wonder why this code never runs when I release a key.
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
MessageBox.Show("It works!");
}
In designer this code is added as usual:
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
However events like Form load works perfect. Is there any common solutions for this 开发者_开发知识库problem?
You should set the KeyPreview
property of the form to true, this should work with your code.
If the form has other controls on it try setting the Form.KeyPreview property to true: Key Preview on MSDN
You have to be sure that there aren't any active control on the current form. Put this code in Form.cs
this.ActiveControl = null;
精彩评论