Hi All,
I am using the input from a TextBox to filter information displayed in a ListBox. I want the filtering to occur each time a key is pressed.
The problem I am faced with is that if the user types a second, third etc. character, the filter will carry on through for each of the key presses. E.g. The user types 'a' and the filtering starts, before filtering for the letter 'a' is finished, the user adds the letter 'b' so the complete search string is now 'ab'.
At this point, what I want to happen is for the first filter to stop completely and start the new filter on 'ab' from scratch.
private bool Interrupt = false;
private bool Searching = true;
private void tbxFilter_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Searching)
{
Interrupt = true;
}
}
private void tbxFilter_KeyUp(object sender, KeyEventArgs e)
{
Searching = true;
foreach (MyObject o in ListOfMyObjects)
{
if (Interrupt)
{
Interrupt = false;
break;
}
else
{
DoFilter(o);
}
}
Searching = false;
}
The code above shows roughly what I have at the moment with some application specific pieces removed (like Types and object names).
What i would like ideally to do is be able to pause at the beginning of the second key press (until Searching has been set to false - indicating that all other searches have exited), but adding a while loop to check for that will just lock the program at that point.
Is there a way to a way to accomplish this?
Thanks
EDIT: The control that is displaying the items is a ListBox / ListView control.