Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
2.00/5 (9 votes)
See more:
How can I force a textchanged event to fire after user finishes writing a string. I don't want it to fire after each char in entered.
Posted
Updated 12-Apr-11 10:50am
v2
Comments
Toniyo Jackson 12-Apr-11 7:53am    
winforms or web?
Olivier Levrey 12-Apr-11 8:36am    
If you want better answers, you should define this: after I finish writing my string. Is it finished once you click somewhere?
Sergey Alexandrovich Kryukov 12-Apr-11 14:05pm    
I decided to put a whole Answer based on some absurdity of this part of text. Please see.
--SA
Sergey Alexandrovich Kryukov 12-Apr-11 14:06pm    
Tag your Question! WinForms, WPF, ASP.NET, what!
Too bad you did not. Do you want us to read your mind?
--SA

You can try Leave event.

This will fire when the cursor leave(lost focus) from textbox.

Note: This solution is for winforms.
 
Share this answer
 
v3
Comments
Toniyo Jackson 12-Apr-11 7:52am    
This solution is for winforms.
#realJSOP 12-Apr-11 7:53am    
The OP hasn't specified the platform, so until he does, this is the answer.
Olivier Levrey 12-Apr-11 8:37am    
I agree. This answer is correct and doesn't deserve a 1 (even though OP would say it is for web).
Have a 5 Toniyo.
Toniyo Jackson 12-Apr-11 8:44am    
Thanks Olivier :)
Member 8153852 26-Aug-15 0:45am    
Lighten up Francis.
The Question makes no sense by the reason Olivier pointed out.
Computer cannot read your mind, that's it!
You need to either handle each end every TextChanges or handle something else as well, period.

Be logical.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:24pm    
I predicted this will be down-voted! What a waste!
--SA
wooga111 12-Apr-11 14:35pm    
Calm down - be nice!
Sergey Alexandrovich Kryukov 12-Apr-11 14:43pm    
??? who is excited?
--SA
Sergey Alexandrovich Kryukov 12-Apr-11 14:44pm    
Be logical.
--SA
Ammar Shaukat 26-Dec-17 1:43am    
Question make sense . But it is also correct , computer cannot read our mind. So I agree with -SA
C#
// Write these lines anywhere in your Form's class:
private Timer timer;

private void DoTick(object sender, EventArgs e)
{
    // Do here what you wanted to do in TextChanged;
    Timer.Stop();  // This line must be here, at the end of the method
}

private void TextChanged(object sender, EventArgs e)
{
    timer.Stop();
    timer.Start();
}

// Write these lines in the Form's constructor, [ex: public Form1()]:
timer = new Timer();
timer.Interval = 250;  // Whatever... Milliseconds (you may increase or decrease this value to suit your needs)
timer.Tick += DoTick;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 14:11pm    
Will work somehow, but the idea pretty is bad, in my opinion. Why this time, not that?
I appreciate your inventiveness though, but cannot vote for that. The final result of it would not be satisfactory. Best thing in programming in not to be a "problem solver" but design proper functionality instead. The problem itself is artificial.
And I'm pretty much sure you understand it yourself. (Well, I say so much just to excuse non-voting :- But this is because I generally value your knowledge and opinion :-)

OK, please see my Answer; I think this is what OP needs the first (I predict heavy down-votes).
--SA
Toli Cuturicu 12-Apr-11 15:51pm    
The OP asked something quite illogical, so I simply gave an appropriate absurd (but tangentially working) example...
Sergey Alexandrovich Kryukov 12-Apr-11 16:32pm    
I vote 5 to compensate another idiotic vote of 1, but mostly in response to your agreement with absurdity.
I suggest you add the notion of absurdity to you Answer though...
--SA
Olivier Levrey 13-Apr-11 4:15am    
I agree. Using a timer here is a bit strange, but so is the original question... I can't vote 5 because of the reason mentionned by SA, but I can't leave this answer downvoted, so I will vote 4.
Sergey Alexandrovich Kryukov 13-Apr-11 4:36am    
Decisions, decisions... I paid attention for some inventiveness of this answer.
By the way, did you see my own Question only one so far?
This is my initiative called [Completely Useless Challenge]. Here:
http://www.codeproject.com/Questions/165416/Completely-Useless-Challenge-Mouse-hating-UI.aspx
I got couple more ideas...
--SA
Validating or Validated event will do the trick for you.
Use Validating event if you wish to manipulate input; Validating can be canceled.
Validated event is enough if you do not need validation and in IMHO it better suits your question.


myTextBox.Validating += new CancelEventHandler(myTextBox_Validating);

void myTextBox_Validating(object sender, CancelEventArgs e)
{
     //Do something
     //You can cancel event and return to editing.
}


myTextBox.Validated += new EventHandler(myTextBox_Validated);

void myTextBox_Validated(object sender, EventArgs e)
{
     //Fire away! You're done with editing text box! :)
}
 
Share this answer
 
Comments
Oshtri Deka 12-Apr-11 17:35pm    
uuuuu, I got one!
Olivier Levrey 13-Apr-11 4:16am    
Somebody decided to heavily vote 1 here... Your answer makes perfect sense. My 5.
you might find it better to put your code at the validating event.
 
Share this answer
 
Comments
Mohammed Ahmed Gouda 12-Apr-11 7:51am    
how ? sorry i am an amatuer
Sergey Alexandrovich Kryukov 12-Apr-11 14:31pm    
The Answer does not say you're experiences. It says: "you might find it better to put your code at the validating event". How you note is related? Be logical!
--SA
a1mimo 12-Apr-11 7:54am    
all you have to do is to select the textbox and see the see properties window you will find the validating event
I insist the Question is very bad and makes no sense.
Down-voting against this opinion does not help!

It's not good for OP: not accepting logical reason leads to all kinds of failures.
What to down-vote again? It would really show the attitude.

Sorry,
—SA
 
Share this answer
 
Comments
Olivier Levrey 13-Apr-11 4:18am    
Agree. My 5. Accepting bad design is non-sense.
Sergey Alexandrovich Kryukov 13-Apr-11 4:40am    
Thank you, Olivier.
Somebody does not understand that...
--SA
Sergey Alexandrovich Kryukov 13-Apr-11 4:41am    
Did you see this:
http://www.codeproject.com/Messages/3854264/Re-QA.aspx
--SA
Olivier Levrey 13-Apr-11 5:41am    
He he no I didn't. It looks like some of us are sometimes loosing faith. Bad experiments are not easy to forget, it is easier to focus on them. Fortunately, they are few (well in my case at least). So I prefer just to leave them where they are. I am an optimistic guy :)
Sergey Alexandrovich Kryukov 13-Apr-11 6:20am    
Do you mean "not well informed"? :-)
--SA
Why didn't you try the LostFocus Event of TextBox?
I think, this is the expectation by you.

TextBox.LostFocus += new RoutedEventHandler(TextBox_LostFocus);

void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
//Do your coding here
}
Have you ever tried this?

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.lostfocus.aspx

Thanks,
Loganathan
 
Share this answer
 
v2
That's going to be a bit difficult as the name of the event suggests it fires every time the text changes. try handling the OnBlur event instead.
 
Share this answer
 

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