Capturing change to TextBox

  • Thread starter Thread starter JezB
  • Start date Start date
J

JezB

I want to simply capture changes to a TextBox and perform some processing
when the user has edited the text.

The TextChanged event fires even while the user is editing the text, but I
only want to perform my validation when the user finished editing the text
and moves off the field (tab, enter, mouse-click somewhere else, etc).

The Validated event fires when the user moves off the field but fires even
if the user does not change the text!

I know I could save the "before value" of the text in a local variable in
the Enter trigger and compare this to the current value in the Leave or
Validate trigger but this is messy! There must be a simpler way surely.

Someone please help!
 
I want to simply capture changes to a TextBox and perform some processing
when the user has edited the text.

The TextChanged event fires even while the user is editing the text, but I
only want to perform my validation when the user finished editing the text
and moves off the field (tab, enter, mouse-click somewhere else, etc).

The Validated event fires when the user moves off the field but fires even
if the user does not change the text!

I know I could save the "before value" of the text in a local variable in
the Enter trigger and compare this to the current value in the Leave or
Validate trigger but this is messy! There must be a simpler way surely.

Someone please help!

No, I'm quite sure there isn't a simpler way. But you could inherit your
own textbox and encapsulate this behavior and even implement such an event.
That would not be that difficult.
 
Validating event. (in combo with Validated)
data is ideally stored after it is validated
 
JezB said:
I want to simply capture changes to a TextBox and perform some processing
when the user has edited the text.

The TextChanged event fires even while the user is editing the text, but I
only want to perform my validation when the user finished editing the text
and moves off the field (tab, enter, mouse-click somewhere else, etc).

The Validated event fires when the user moves off the field but fires even
if the user does not change the text!

Take a look at the textbox' 'Validating' and 'ModifiedChanged' events and
its 'Modified' property.
 
Thanks, this solution worked the best:

private void localRoot_Validating(object sender, CancelEventArgs e)
{
if (localRoot.Modified)
{
...
}
}
 
Back
Top