Capturing change to TextBox

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!
 
C

Claudio Grazioli

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.
 
J

joeycalisay

Validating event. (in combo with Validated)
data is ideally stored after it is validated
 
H

Herfried K. Wagner [MVP]

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.
 
J

JezB

Thanks, this solution worked the best:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top