这个问题有点晦涩,但是我找不到任何地方的答案。我正在用 C# (Visual Studio Pro 2013) 编写一个程序,我需要在用户停止输入 2 秒后执行一个操作(将间隔设置为 2000)。为此,我需要一个标准计时器,但是我需要检测用户何时停止输入 2 秒。我该怎么做呢?
最佳答案
完整代码如下:
public partial class Form1 : Form
{
System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
// Initialize the timer.
timer = new System.Timers.Timer();
timer.Interval = 2000; // = 2 seconds; 1 second = 1000 miliseconds
timer.Elapsed += OnElapsed;
}
// Handles the TextBox.KeyUp event.
// The event handler was added in the designer via the Properties > Events > KeyUp
private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
// Reset the timer on each KeyUp.
timer.Stop();
timer.Start();
}
private void OnElapsed(object source, ElapsedEventArgs e)
{
// When time's up...
// - stop the timer first...
timer.Stop();
// - do something more...
MessageBox.Show("Time out!");
}
}
关于c# - 检查非打字间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27670972/