Check spacing without entering text

This question is a bit unclear, 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 to 2000). For this I need a standard timer, but I need to detect when the user stops typing within 2 seconds. How can i do this?

+3


source to share


5 answers


I just found the correct answer, simple and compact. I'll explain this first and then show you some code example. You need to turn off the timer and then you need to turn it on as soon as the user presses a key. Then, while still in the method for the user pressing the button, you would need to reset the timer interval to 2000. When the timer expires, you perform the action that should happen when the keyboard is inactive for 2 seconds. Sample code:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    this.timer.Enabled = true;
    this.timer.Interval = 2000;
}

      



Now the timer_Tick method:

 private void timer_Tick(object sender, EventArgs e)
 {
    //Do something when the keyboard has been inactive in 2 seconds
 }

      

0


source


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 up...

        // - stop the timer first...
        timer.Stop();

        // - do something more...
        MessageBox.Show("Time out!");
    }        
}

      

+2


source


Try the following:

System.Timers.Timer timer = new System.Timers.Timer(2000);;
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer.Enabled = false;

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
    timer.Enabled = false;
    timer.Enabled = true;   
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    // Do something
    timer.Enabled = false;
}

      

0


source


king.code's answer is correct if you just reset the timer on the first line of the textBox1_KeyUp event and initialize the timer in the constructor or main method depending on your usage.

System.Timers.Timer timer;

private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
    timer.Stop();
    timer.Start();
    timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    // Do something
}

      

I would recommend that you go for a class that inherits the textbox and passes a timer to it if you are going to use it in multiple places

0


source


It is easy to create a timer that sets 2000 Ms and processes it by changing the text interversionally. Example: Set an integer from 0 to 2000, then increase it to Timer if the user starts typing, then reset the rest number to keep Counting to 2000 ping, rusty master helped;)

0


source







All Articles