Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.

I am trying to make a dynamic search text box. For the search box I use timer and Textchanged event.
The way i found out is: Lazy pattern, means keep the last time of text changed, then in another thread (like timer) check if there is more than 2 second between last user change to current time, update your search: (timer interval is 2 second).

Initializer of the timer and it is in the constructor of the form.
C#
Timer timer = new Timer();

private void InitTimer()
{
    timer.Tick += new EventHandler(timer1_Tick);
    timer.Interval = 5000;
    timer.Start();
}

The variables I use in the events.
C#
private DateTime lastChange = DateTime.Now;
private bool textChanged = false;
object lockObject = new object();

Here's the textchanged event:
C#
private void textChanged(object sender, EventArg e)
{
   lock(lockObject)
   {
      lastChange = DateTime.Now;
      textChanged = true;
   }
}


C#
private void timer1_Tick(object sender, EventArgs е)
{
    lock(lockObject)
    {
       if (textChanged && lastChange > DateTime.Now.AddSeconds(-2)) // wait 2 second for changes
       {
          DoSearch();
          textChanged = false;
          lastChange = DateTime.Now;
       }
    }
}

private void DoSearch() 
{ 
    timer.Stop();
    MessageBox.Show("results");
    //Do search
}

The problem is that when I start typing the timer is started and begins showing bunch of message boxes around the screen.
Posted
Comments
BillWoodruff 31-Oct-14 3:35am    
We see the elements of a solution here, but a solution to what: what is 'DoSearch() doing ?

What are the conditions in which you will not display a 'MessageBox in the 'DoSearch Event ?

And, why are you using a MessageBox which is modal, and will pause the app until it is closed ?
Robert Welliever 31-Oct-14 3:55am    
Why don't you set the timer for a two second interval instead of five, and then store the value of the textbox on each poll? You can then ignore the check for textChanged and lastTime > two seconds ago, and just check that the strings aren't equal. You see, your current implementation will do a search in cases where the text hasn't changed, but the event was fired (e.g. I press a key and then the backspace to delete it). Also the five second delay and the two second check are at odds with each other, just doesn't make sense. Also the lockObject doesn't make any sense as there is no concurrency issues.

Um.
That isn't your code, or it doesn't compile, and you haven't noticed.
This:
C#
private bool textChanged = false;
This:
C#
private void textChanged(object sender, EventArg e)
{
   ...
And
C#
if (textChanged && lastChange > DateTime.Now.AddSeconds(-2)) // wait 2 second for changes
{
   ...
Will not compile as there is an ambiguity between the two names. (And it's EventArgs, not EventArg in the event handler declaration.

So start by getting it to compile, and then work from there. We can't do anything without the code you are actually running!
 
Share this answer
 
Here, this is what I meant by my above comment:

C#
Timer _InputTimer = new Timer();
string _Input = "";

private void _InitTimer()
{
    _InputTimer.Tick += new EventHandler(_InputTimer_Tick);
    _InputTimer.Interval = 2000;
    _InputTimer.Start();
}

private void _InputTimer_Tick(object sender, EventArgs е)
{
    if (_Input != txtInput.Text){
        _InputTimer.Stop();
        _Input = txtInput.Text;
        _DoSearch();
    }
}

private void _DoSearch()
{
    MessageBox.Show("Now's where you might multi-thread");
}
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900