c# - 检查非打字间隔

标签 c# winforms

这个问题有点晦涩,但是我找不到任何地方的答案。我正在用 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/

相关文章:

c# - 有没有办法简化具有多对一关系的 Linq 查询?

c# - 在 Dapper 的上下文中不允许使用可枚举的参数序列(数组、列表等)

c# - 检测 TextBox 中的 Tab 键按下

c# - Lazy Operator C# 用于自己的数据访问

c# - MVP 依赖注入(inject)

c# - 更快的鼠标点击响应?

c# - 关于 C# 连接到 EC2 的问题

c# - 文本框/输入文本中的 HTML 标记

c# - 将值从窗口返回到 WPF 类

c# - 如何从 Program.cs main 方法更改 WinForms 标签的内容?