c# - Why isn't this causing an infinite loop of events? -


i have simple application reverses text typed in textbox. catch is, can modify either textbox , changes (literally) reflected in other.

i wrote code, believing cause problems.

private void realtext_textchanged(object sender, eventargs e) {     mirrortext.text = mirror(realtext.text); }  private void mirrortext_textchanged(object sender, eventargs e) {     realtext.text = mirror(mirrortext.text); }  private string mirror(string text) {     return new string(text.reverse().toarray()).replace("\n\r", "\r\n"); } 

i tried out, believing cause infinite loop (realtext changes mirrortext, event happens, mirrortext changes realtext, etc). however, nothing except intended behavior happened.

i'm of course happy this, leave here. or i?

i'm quite sure textchanged event supposed fired whenever text changed. intended behavior of error protection in events, or lucky? can code misbehave on computer, other build settings, etc? can fixed:

private void realtext_textchanged(object sender, eventargs e) {     if (realtext.focused)     {         mirrortext.text = mirror(realtext.text);     } } 

i'll anyway safe, required check this? (i'm not going ask if it's recommended.)

per comments, , answered, textchanged event not getting raised when set text property value has.

it's not clear whether can safely rely upon. sensible optimisation, , surprised if future versions of .net framework drop it, cannot speak older versions, nor third-party implementations (mono).

to absolutely safe, not use focused check put in question. exactly text setter now.

private void realtext_textchanged(object sender, eventargs e) {     var newmirrortext = mirror(realtext.text);     if (mirrortext.text != newmirrortext)         mirrortext.text = newmirrortext; } 

this has same advantage of preventing infinite recursion, plays more nicely other code may put in form changes text result of other event.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -