developer
developer

Reputation: 5478

Textbox value changed

Is it possible to know if any of the textbox values have changed in the application. I have around 30 textboxes and I want to run a part of code only if, any of the textboxes value has changed out of the 30. Is there a way I can know that.

Upvotes: 6

Views: 47771

Answers (5)

Kishore Kumar
Kishore Kumar

Reputation: 21863

try this. Add this code to the load/constructor. no need to specify the event in the XAML explicitly

this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextChanged));
private void TextChanged(object Sender, TextChangedEventArgs e)
{
    //ToDO (use sender to identify the actuale text from where it fired }
}

Upvotes: 1

Remi Smirra
Remi Smirra

Reputation: 2539

You can also just do this:

In your Constructor:

MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );

And Then this Method:

private void TextChanged(object Sender, TextChangedEventArgs e){
            //Do something
        }

Upvotes: 1

ChrisF
ChrisF

Reputation: 137148

Each text box will raise an event TextChanged when it's contents have changed. However, that requires you to subscribe to each and every event.

The good news is that you can subscribe to the event with the same method multiple times. The handler has a parameter sender which you can use to determine which of your 30 text boxes has actually raised the event.

You can also use the GotFocus and LostFocus events to keep track of actual changes. You would need to store the original value on GotFocus and then compare to the current value on LostFocus. This gets round the problem of two TextChanged events cancelling each other out.

Upvotes: 12

SeaDrive
SeaDrive

Reputation: 4292

This is perhaps on the rough and ready side, but I did it this way.

In the constructor, I created

bool bChanged = false;

In the TextChanged event handler of each control (actually same for each), I put

bChanged = true;

When appropriate, I could do some processing, and set bChanged back to false.

Upvotes: 1

dthorpe
dthorpe

Reputation: 36082

You can assign an event handler to each of the TextBox's TextChanged events. All of them can be assigned to the same event handler in code. Then you'll know when the text changes. You can set a boolean flag field in your class to record that a change occurred.

Upvotes: 2

Related Questions