Whats the best way/event to use when testing if the textbox text has finished a change of text
using winforms, c#, vs 2008
So i have textbox1, textbox2 and textbox3 on a winforms.
Textbox3.text = textbox1.text +开发者_运维技巧 textbox2.text.
I need textbox3 to be updated whenever the contents of textbox1 and textbox2 have been changed either manually or programmatic.
The problem is if i use textbox textchanged event it keeps firing as one types in the textbox. I need a way to call my method to fill textbox3 after either tb1 or tb2 have been FINISHED changing programmaticly or via key entry, and not fire everytime a letter of text is entered.
How can I have TextBox3 update only when tb1 or tb2 have finished changing?
It depends a little on what you mean by finished.
The most common choice is to run an event when the focus leaves the control. That's the Control.Leave event or Control.LostFocus. These are also the events that are used for validation - validation of a new value (Validated
and Validating
events) takes place when the focus is changed.
If you're trying to do something like a progressive search, where you need to respond while the control is still in focus but not respond to every key press, then another solution is to use a Timer component, and restart the timer every time the TextChanged
event is fired. This way, your update code will only be run after the user changes the text and hasn't typed anything else for, say, 1 second.
I'm not exactly sure if this is what you are after, but DevExpress' editors control provides a buffered option which fires the change event after a user finishes typing (instead of after every keystroke)...this is basically what Aaronaught is suggesting with a Timer component...
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraEditorsRepositoryRepositoryItem_EditValueChangedFiringModetopic
There are several methods that you can use: Leave
event, or a manual "typing stopped" event.
The Leave
event is generally a good one for processing user input (for validation for example) as they move on to another part of the form. Just make sure that the event fires if they go from the textbox to any other UI element on your form - you may need to force a focus on the new element.
The TypingStopped
event is something you would need to create yourself, but the basic idea of it is a short duration timer (say 500ms, but you would need to test it), which you restart on every KeyDown event of the TextBox. The timer would fire its own event and disable itself if it ever hits the end of it's timeout.
The answer is to use the KeyPressUp event. This event is triggered after a character is either added or removed from the text box. Thus you can check the length of the text of the textbox and set either enabled or visible attributes based on text lengths.
精彩评论