#633 – TextChanged Event Fires After TextBox Text Has Changed

You can intercept the PreviewKeyDown event for a TextBox to look at each key pressed while the TextBox has focus.  You can also intercept the PreviewTextInput event to look at text that is being entered into the TextBox.  However, these events still won’t handle all possible changes to the text in the TextBox.  For example, you might right-click in the TextBox and select Paste to paste some text.

To see all changes to the text in a TextBox, you can handle the TextChanged event.  This event fires after the text has changed.

In the TextChanged event, the easiest way to inspect the text being entered is to just look at the value of the Text property.  This will contain the new contents of the TextBox.

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Trace.WriteLine(string.Format("TextBox_TextChanged, Text now = [{0}]", ((TextBox)sender).Text));
        }

For example, if I just enter the text “Harpo”:

 

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a comment