开发者

Dynamic Search Result C#

开发者 https://www.devze.com 2023-02-01 05:24 出处:网络
i\'m try to develope a list of result by a dynamic search in a text box. Now i update the record when:

i'm try to develope a list of result by a dynamic search in a text box.

Now i update the record when:

        SearchBox.TextChanged += new Ev开发者_运维技巧entHandler(SearchBox_TextChanged);`

But i have to wait every char that i write for the complete result of the list.

so if i search for example "com" the result is not words that contains "com" but only "c".

For have the result of "com" i need to write: "c" -> Wait to complete search "o" -> Wait to complete search "m" -> Wait to complete search

How can i do for wait some time that the user write the word and then search?

Thanks.


You are not aware what user want to insert in text box, so may be is Cat, Contact, Com, Computer,... So you should restrict the search list by each keyword from user, also you can set a policy for example just search when the input length is at least 3.

Edit: The other way is to use 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).

private DateTime lastChange = DateTime.Now;
private bool textChanged = false;
object lockObject = new object();

private void textChanged(object sender, EventArg e)
{
   lock(lockObject)
   {
      lastChange = DateTime.Now;
      textChanged = true;
   }
}

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


the easiest way is to add into the event handler a condition like a minimum length

 private void SearchBox_TextChanged(Event e,...){
   if(e.text.Length > 3)
      search(e.text);
  }


Use a Timer. On every TextChanged event,

  • start that timer if not running,
  • restart (stop & start) if already running,

On Timer's Tick event, write your code to update Search Results and stop timer.

This will give your users some time to write complete search key-words.

Usually Timer Interval can be around 1 Second.


You can create a Timer when your application starts, and then on every keystroke simply reset and re-start the timer.

When the user stops typing, the timer will execute and perform the search.


You can use timer with an Interval of some like 2 seconds and implement search within timer's Tick event. Enable timer within TextChanged event and once search completed then disable timer within Tick event.

0

精彩评论

暂无评论...
验证码 换一张
取 消