首页 > 代码库 > c#问题(按F1或F2键时触发事件)

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的属性中设定的。

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