views:

137

answers:

3

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?

A: 

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.

drs9222
A: 

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

Matthew Scharley
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
Then even though drs9222 got downvoted, this is all you can do. Binding's shouldn't be updated before validation passes though.
Matthew Scharley
Isn't there any method that allows you to halt the binding, and other to update the bindings?
devoured elysium
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
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
A: 

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.

Pat