Search code examples
c#.netwinformseventstextchanged

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


I have a simple application that reverses any text typed to it in another textbox. The catch is, you can modify either textbox and the changes will be (literally) reflected in the other.

I wrote this code, believing for it to 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 then tried it out, believing that it would cause an infinite loop (realText changes mirrorText, another event happens, mirrorText changes realText, etc). However, nothing except the intended behavior happened.

I'm of course happy about this, I could just leave it here. Or could I?

I'm quite sure the TextChanged event is supposed to be fired whenever Text is changed. Is this intended behavior of some error protection in the events, or was I just lucky? Can this code misbehave on another computer, with other build settings, etc? It can be easily fixed:

private void realText_TextChanged(object sender, EventArgs e)
{
    if (realText.Focused)
    {
        mirrorText.Text = Mirror(realText.Text);
    }
}

I'll probably do it anyway to be safe, but is it required to check this? (I'm not even going to ask if it's recommended.)


Solution

  • Per the comments, and as already answered, the TextChanged event is not getting raised when you set the Text property to the value it already has.

    It's not clear whether this is something you can safely rely upon. It is a sensible optimisation, and I would be very surprised if future versions of .NET Framework drop it, but I cannot speak for older versions, nor for third-party implementations (Mono).

    To be absolutely safe, I would not use the Focused check you put in your question. I would do exactly what the Text setter does now.

    private void realText_TextChanged(object sender, EventArgs e)
    {
        var newMirrorText = Mirror(realText.Text);
        if (mirrorText.Text != newMirrorText)
            mirrorText.Text = newMirrorText;
    }
    

    This has the same advantage of preventing infinite recursion, but plays more nicely with other code you may put in your form that changes the text as a result of some other event.