Take the 2-minute tour ×

I am binding a textbox's text value setting to my a variable in my configuration file. I only consider the input from this textbox to be valid if it's an integer number bigger than 1. Right now what I was doing is letting the user write whatever he wanted in the box, and I'd only let him save the settings after calling a validation function. The problem seems to be that my binding variable in the Settings class seems to be being updated as soon as the textbox is being edited, and that's a behaviour i'd like to skip. How can I do this?

share|improve this question
add comment

3 Answers

You can use the OnValidating event on the textbox to do your validation code, and set e.Cancel to true if the validation doesn't pass (and inform the user somehow, perhaps a MessageBox).

share|improve this answer
It doesn't look as if the Validating event has anything to look with the bindings. It looks to happen when your control loses focus. The problem will still be the same, as when you bind your control it will just update the binding variable as soon as you type whatever you want in the control. I don't want to force my user to have to write everything right at the first time, I just want the binding variables to be all correct when the user presses the Save button(and then it runs the validation code). –  devoured elysium Jul 22 '09 at 23:25
Then even though drs9222 got downvoted, this is all you can do. Binding's shouldn't be updated before validation passes though. –  Matthew Scharley Jul 22 '09 at 23:29
Isn't there any method that allows you to halt the binding, and other to update the bindings? –  devoured elysium Jul 22 '09 at 23:34
I mean, for what do this configuration settings classes serve, then? If we can't validate data before the data before the configuration setting's classes bind info the binding variables, what's their use? I can't find any. –  devoured elysium Jul 22 '09 at 23:37
You can validate data before it makes it to the binding; using the builtin validation methods. If you don't want to use the built-in validation methods, then you can't use the built-in binding methods. Simple as that. Luckily, it's fairly trivial to do your own bindings if you really want to go down this route. Configuration settings are useful for persisting settings. How you edit them is totally up to you. –  Matthew Scharley Jul 23 '09 at 1:11
add comment

Don't bind the value to the textbox. First do as Matthew says and "use the OnValidating event on the textbox to do your validation code" and then use the OnValidated event to update the configuration value.

share|improve this answer
add comment

You might be able to use a MasktedTextBox (with or without a mask) and then subscribe to the Validating (or TypeValidationCompleted) event to add your custom validation. I believe that the bound value is only saved after validation has completed successfully.

share|improve this answer
add comment

Your Answer


