I wish to use the Validating event on a label in VB.Net (Visual Studio 2005, .Net Runtime 2.0).
I have two text boxes side by side. These are for a %/% split allocation, such as 80/20, 50/50, etc. So, I have validation on each box to ensure that their values are >= 0 and <= 100. It is my desire to have the label off to the left of the textboxes have its own validation, which will ensure that the two text box values total exactly 100.
I know I can do this in the individual text box Validating event. I just do not want that behavior. I would rather have a discrete message on each text box, stating that its contents are outside the valid domain of values, and have an error message by the label if their total is not 100.
The problem is that, despite setting CausesValidation = True on the Label controls, their Validating events do not fire. Even manually calling Me.ValidateChildren() at the form level will not fire the event for labels.
Any ideas?
validation events are for controls that have editable values. labels do not have editable values.
your situation is not uncommon, you have a field-level validation that says ‘positive integer’ and a business rule that says the sum of values A and B must equal 100%.
one way to handle this is to delay enforcing the business rule until the form is completed; this would entail validating the business rule when the user clicks the OK button (or Submit button in a web form)
if you want instant validation, just have the validation method for both fields call the business-rule validation method