开发者

KeyUp event on form wont run? c#

开发者 https://www.devze.com 2023-01-19 10:03 出处:网络
I wonder why this code never runs when I release a key. private void Form1_KeyUp(object sender, KeyEventArgs e)

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消