No event label

I want to use Validating event on a label in VB.Net (Visual Studio 2005, .Net Runtime 2.0).

I have two text boxes side by side. They are for a% /% split distribution like 80/20, 50/50, etc. So, I have a validation on each field to make sure their values ​​are> = 0 and <= 100. It's my desire to have a label to the left of the text fields, has its own validation that ensures that the two text field values ​​are exactly equal one hundred.

I know I can do this in a separate text box. I just don't want this behavior. I would rather have a discrete message in each textbox, stating that its content is outside the valid domain of values, and has an error message with a label if their sum is not 100.

The problem is that even though CausesValidation = True on Labels, their validation events are not firing. Even a manual call to Me.ValidateChildren () at the form level does not raise an event for labels.

Any ideas?

+1


source to share


4 answers


validation events for controls that have editable values. labels have no 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 the values ​​A and B must be 100%.



One way to deal with this is to defer the execution of the business rule until the form is complete; this will validate the business rule when the user clicks the OK button (or the submit button in a web form).

if you want instant validation just use validation method for both fields to call business rule validation method

+2


source


May I ask if this is a web project or a project?



The user cannot enter any data in the shortcut, so I don't understand why they will warn about errors.

0


source


Look at the "with changed text" event, I think you don't understand the use of validation in case of controls.

In the method that handles the "text change" event for tb1, you can put code that, when editing the change value in tb2, will add 100, and vice versa, editing tb2 will cause an automatic change in tb1. You can also enable this reaction of event handlers for invalid values

care

0


source


As per the documentation for CausesValidation:

"Gets or sets a value that indicates whether the control will perform validation on any controls that require validation when it receives focus."

In my own testing, I had to specifically call Label.Focus () and then exit that field (or call another .Focus () control) to fire the event.

I really think you are using the wrong methodology to get the results you are looking for. I think it would be better if the validators on the textarea call a utility method that sets the label appropriately.

0


source







All Articles