Get value of TextBox if 1s elapsed after last change c# -
how can value of textbox 1seconde after last change .
i tried stopwatch , timerstamp time between 2 change don't know how value of textbox 1 seconde after.
thanks help!
edit:
stopwatch timerbetweenwrite = new stopwatch(); private void textbox_textchanged(object sender, textchangedeventargs e) { timerbetweenwrite.stop(); // elapsed time timespan value. timespan ts = timerbetweenwrite.elapsed; if (search.text != null && ts.seconds >= 1) { //doing stuff } timerbetweenwrite.restart(); }
but don't work want because need change textbox 1 seconde after last change. want run function 1 seconde after last change of textbox user can continue change textbox.
final edit:
that code work thank's help!
public partial class viewerpage : page { system.timers.timer mytimer = new system.timers.timer(1000); public viewerpage() { initializecomponent(); mytimer.elapsed += new elapsedeventhandler(mytimer_elapsed); } private void textbox_textchanged(object sender, textchangedeventargs e) { mytimer.stop(); //reset timer mytimer.start(); //restart } private void mytimer_elapsed(object sender, elapsedeventargs e) { threadcontext.invokeonuithread( delegate() { // doing stuff mytimer.stop(); }); } } public static class threadcontext { public static void invokeonuithread(action action) { if (application.current.dispatcher.checkaccess()) { action(); } else { application.current.dispatcher.invoke(action); } } public static void begininvokeonuithread(action action) { if (application.current.dispatcher.checkaccess()) { action(); } else { application.current.dispatcher.begininvoke(action); } } }
timer mytimer = new timer(1000); mytimer.elapsed += mytimer_elapsed; private void textbox_textchanged(object sender, textchangedeventargs e) { mytimer.stop(); //reset timer mytimer.start(); //restart } private void mytimer_elapsed(object sender, elapsedeventargs e) { //do stuff }
explanation: each time text changes, timer gets reset , started again. tick if it's enabled (aka not stopped textchanged
event) second. if want tick once , stop, set autoreset
property true.
Comments
Post a Comment