What is the difference between TextUpdate and TextChanged Event?
Asked Answered
P

2

17

for each control there are a lot of events, two are very similar such as Text Update and Text Changed, whis is the difference?

Paean answered 13/2, 2014 at 21:11 Comment(1)
Whats the context here? What kind of controls? Winform? WPF? MVC...?Mur
A
16

Here's my take on things, with sources from MSDN. I've used TextBox and ComboBox for my examples, however I'm fairly sure the logic generalizes.

TextUpdate:

Occurs when the control has formatted the text, but before the text is displayed. Use the TextUpdate event to validate the text before it is actually shown.

An example would be if a ComboBox is being populated from some datasource, and the data changes. This could trigger the TextUpdate event to allow for validation (or anything else).

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.textupdate(v=vs.110).aspx

TextChanged:

Occurs when content changes in the text box. User input or setting the Text property to a new value raises the TextChanged event.

I think that quotation covers the example usage.

http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.textchanged(v=vs.95).aspx

Anthologize answered 13/2, 2014 at 21:18 Comment(1)
Remarks page at that first link states: "TextUpdate is not fired if the Text property is programmatically changed" which makes it handy if you e.g. just want to capture typing in the textbox portion of a ComboBox (as described in @damichab's answer below)Somatic
M
2

I have just been playing around with this with the comboBox...

I found that the TextChanged event fires when the text is changed, eg user input or SelectedIndex changed.

TextUpdated event only fires when the user updates the text.

My program, I use Items.Add("x") to populate and SelectedIndex to select row. When user selected different item, the TextUpdated not fired. In TextUpdated, I have code to save the updated text as user is entering/changing it. You have to get the SelectedIndex from SelectionChangedCommitted and save it to a variable though as SelectedIndex within the TextUpdated appears to only return -1.

Myrmeco answered 13/8, 2020 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.