Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for a non typing interval

Tags:

c#

winforms

This question is a bit obscure, however I cannot find an answer for it anywhere. I am writing a program in C# (Visual Studio Pro 2013) and I need to perform an action after the user has stopped typing for 2 seconds (setting the interval at 2000). I would need a standard timer for this however I need to detect when the user has stopped typing for 2 seconds. How would I go about doing this?

like image 703
carefulnow1 Avatar asked Dec 06 '25 08:12

carefulnow1


1 Answers

Here's the complete code:

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!");
    }        
}
like image 85
t3chb0t Avatar answered Dec 07 '25 20:12

t3chb0t



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!