开发者

Textbox changing state

I have a textbox on my form. while I am changing the text but not finished yet, If I add event TextChanged then this event will raise during I am changing the text =>开发者_高级运维 this is not what I expect.

Second situation: instead of using TextChanged, I used text_Validated or text_Validating event, it works ONLY after my mouse pointer to another area than the textbox itself.

so, my question is how can I achieve in such a way that after changing the text without moving the pointer like the TextValidated behaviour, then the textbox event will raise.

I hope you can understand my current problem and give me an advice on how can I solve this problem.


The problem is "When do you know the user is finished?". Did he really finished his entry, is he simply thinking about what he should type next or is he simply searching the right key on the keyboard?

So you can only guess. If this guessing is suitable for you, you could add a Timer to your form. Set it to maybe 1000 ms and reset it on every TextChanged event (switch Enabled to false and back to true again). And to the Tick event simply add your GuessTheUserFinished function.

Sample code

    timerFilterEntering.Interval = 1000;
    timerFilterEntering.Tick += (sender, e) =>
        {
            timerFilterEntering.Enabled = false;
            GuessTheUserFinished(myTextBox.Text);
        }

    myTextBox.TextChanged += (sender, e) =>
        {
            timerFilterEntering.Enabled = false;
            timerFilterEntering.Enabled = true;
        }

void GuesTheUserFinished(string inputSoFar)
{
    // ToDo: Whatever you like to do with the input.
}


Something like this perhaps...

Handle the TextChanged event and add the following in it:

  • Save the timestamp when this happens in a local var.
  • If any previous timestamp has been cached (i.e. in the control Tag property, or wherever), compare the two timestamp and see if x amount of time has passed (delay between the text changes). If it is larger than x amount of time, assumed that the user has finished typing and do whatever you want
  • Cache the latest timestamp again somewhere (in Tag or whatever)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜