简体   繁体   English

捕获空格键按暂停计时器 C#

[英]Capture spacebar press to pause Timer C#

I have a windows form which is a countdown Timer.我有一个 windows 表格,它是一个倒数计时器。 What is the best way for me to pause the timer by pressing the spacebar on the keyboard.通过按键盘上的空格键来暂停计时器的最佳方法是什么? I have three buttons on the form for start, pause and stop and res-set.我在表单上有三个按钮,用于开始、暂停和停止以及重新设置。 However I also want to pause, restart on press off spacebar.但是我也想暂停,按下空格键重新启动。 I tried to add a fourth button and then made it hidden on the form and added the following code (changed the EventArgs to KeyEventArgs:我尝试添加第四个按钮,然后将其隐藏在表单上并添加以下代码(将 EventArgs 更改为 KeyEventArgs:

    private void button1_Click(object sender, KeyEventArgs e)
    {
       if (e.KeyCode == Keys.Space)
           // Pause the timer.  
       timer1.Enabled = false;
       paused = true;
       Start.Enabled = true;
       Pause.Enabled = false;  
    }

However when i try to run this i get the error - No overload for 'button1_Click matches delegate System.EventHandler但是,当我尝试运行它时,我得到了错误 - 'button1_Click 没有重载匹配委托 System.EventHandler

Is there something I have missed or a better way off doing this.有什么我错过的或更好的方法吗?

Any Help/Advice would be appreciated.任何帮助/建议将不胜感激。

The Click event doesn't take a KeyEventArgs . Click事件不需要KeyEventArgs

You're looking for the form's KeyPress event.您正在寻找表单的KeyPress事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM