我想要下一個: 當您按空格鍵 - label1.Text成爲「向上」,幾秒鐘後(隨機從1到5)label1.Text將更改爲「刪除手」,然後KeyUp label1.Text將更改「下」。 我知道如何使用KeyUp和KeyDown,但我不明白如何使用計時器?如何使用鍵盤事件和計時器?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Timer timer = new Timer();
public Form1()
{
InitializeComponent();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
label1.Text = "Down";
timer.Interval = 5000;//5 seconds
timer.Tick += new EventHandler(timer1_Tick);
timer.Start();
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
label1.Text = "Up";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = "Remove";
timer.Stop();
}
}
}
你到目前爲止試過的是什麼? –
你有什麼嘗試?看看[timer](http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx)類。特別是Elapsed事件可能會引起你的興趣。 – Skalli
我試過這個..但它沒有工作。見上面的代碼。 – annayak