我想要下一个:当你按空格键时 - 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();
}
}
}