c#问题(按F1或F2键时触发事件)

this.KeyPreview = true;



private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {

            if (e.KeyCode == Keys.F1)
            {
                // Display a pop-up Help topic to assist the user.
                MessageBox.Show("你好");
            }
            else if (e.KeyCode == Keys.F2)
            {
                MessageBox.Show("好");
            }
        }

哪位看看,代码有错误吗?为什么我按F1和F2没反应呢?求解

 

        private void Form1_KeyDown(object sender, KeyEventArgs e)        

   {             switch (e.KeyCode)            

    {

                case Keys.F1:                    

        break;                

     case Keys.F2:                    

        break;                

     case Keys.F3:                    

         break;                    

      }            

     MessageBox.Show("key down is " + e.KeyCode.ToString());        

}  

我刚是了下,我这边成功了。语句和你写的一样,只是KeyPreview这个键值我是在form1的属性中设定的。

转载于:https://www.cnblogs.com/xihong2014/p/4182692.html

你可能感兴趣的:(c#)