#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 .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: