TMaddox
TMaddox

Reputation: 87

C# Wpf textbox difference between TextChanged and PreviewTextInput

I am currently implementing a validation for my textbox and I am not sure which Event to choose, TextChanged or PreviewTextInput, as there seems to be no difference.

How are those events different?

Upvotes: 1

Views: 5946

Answers (2)

Ryan K.
Ryan K.

Reputation: 67

The PreviewTextInput event gets fired "when the TextBox gets text in a device-independent manner." The text property of the event argument of PreviewTextInput (which is of type TextCompositionEventArgs) will only contain the text of the last input action. In the case of keyboard input, this will most likely be one key-down and thus only one character. Other input methods, such as voice input, could input more than one character at a time.

The TextChanged event looks at the Text property of a TextBox and occurs when its text changes in any way. Its event argument, of type TextChangedEventArgs, has a collection of TextChange objects that contain information about the changes that have been made.

As for which you should choose for validation, the answer is... neither. Since you wish to validate, I can only assume you will be using the entered text elsewhere in your project. In WPF, an MVVM (that's Model, View, ViewModel) architecture is preferred, which means we use Bindings for input. There are multiple ways of validating the text of a TextBox in MVVM, with my preferred methods being the IDataErrorInfo interface and Binding Validation Rules. The former performs validation after the text data has been sent to the ViewModel, and the latter will validate the text before it's pushed to the ViewModel.

Hope this helps you out!

Upvotes: 0

Dipen Shah
Dipen Shah

Reputation: 26075

Excerpt from framework documentation:

UIElement.PreviewTextInput Event

The PreviewTextInput event allows a component or application to listen for text input in a device-independent manner. The keyboard is the primary means of PreviewTextInput; but speech, handwriting, and other input devices can also generate PreviewTextInput.

Because of key combinations—either in default keyboards or through input method editors—multiple key events may raise just one text input event.

This event creates an alias for the TextCompositionManager.PreviewTextInput attached event for this class, so that PreviewTextInput is part of the class members list when UIElement is inherited as a base element. Event handlers that are attached to the PreviewTextInput event are attached to the underlying TextCompositionManager.PreviewTextInput attached event and receive the same event data instance.

TextBoxBase.TextChanged Event

For a TextBox, this event occurs when its text changes; for a RichTextBox, this event occurs when any content or formatting changes (for example, images, table, or background color).

My 2 cents:

There is a big difference between these two events. TextChanged event will be executed after textbox has processed text and updated control. On the other hand PreviewTextInput event will be executed when you have focus on the textbox and preform action on the input device(keyboard for example) before even textbox has any idea about upcoming input from the device.

Upvotes: 4

Related Questions