Click here to Skip to main content
14,542,448 members
Rate this:
Please Sign up or sign in to vote.
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
Rate this:
Please Sign up or sign in to vote.

Solution 3

You can try Leave event.

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

Note: This solution is for winforms.
   
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.
#realJSOP 12-Apr-11 7:54am
   
5 - Proposed as answer, based on info (or lack thereof) provided buy the OP.
Mohammed Ahmed Gouda 12-Apr-11 8:06am
   
yeah it works put how can i make it lose focus automatically not manual.sorry i know am annoying but i am using this textbox with scanner so i want it after reading each string to put it in DB.
JOAT-MON 12-Apr-11 14:33pm
   
Try using the TextBox.SelectNextControl() function after the scan completes.
Rate this:
Please Sign up or sign in to vote.

Solution 6

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
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 4

// 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;
   
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.
   
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
Olivier Levrey 13-Apr-11 5:33am
   
Yes I saw it. Unfortunately, I have little time to give to such a project, even though it would be funny. My work is taking all my time, that's also why I am not answering so often in CodeProject these days...
   
Absolutely, this is a problem. I'm procrastinating not a very long CodeProject article for some three months already, even though it's 99% ready...
--SA
Sergey Alexandrovich Kryukov 12-Apr-11 14:28pm
   
Ha ha ha! It happened! My Answer is down-voted. I predicted it!
--SA
Toli Cuturicu 12-Apr-11 15:51pm
   
It wasn't me... :-(
Sergey Alexandrovich Kryukov 12-Apr-11 16:33pm
   
He he... no doubt... :-)
--SA
Oshtri Deka 12-Apr-11 15:11pm
   
Why timer? If you don't find available events satisfying you can always write your extended TextBox.
As SAKryukov said it will work somehow, but even a wooden stove works at least once.
Toli Cuturicu 12-Apr-11 15:51pm
   
The OP asked something quite illogical, so I simply gave an appropriate absurd (but tangentially working) example...
Rate this:
Please Sign up or sign in to vote.

Solution 8

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! :)
}
   
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

you might find it better to put your code at the validating event.
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 7

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
   
Comments
Olivier Levrey 13-Apr-11 4:18am
   
Agree. My 5. Accepting bad design is non-sense.
   
Thank you, Olivier.
Somebody does not understand that...
--SA
   
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 :)
   
Do you mean "not well informed"? :-)
--SA
Olivier Levrey 13-Apr-11 7:04am
   
Ah ah ;)
I'd rather say I can turn blind and deaf on demand!
   
This is almost like using control substances. :-)
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 9

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
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100