views:

689

answers:

4

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?

A: 

May I ask is this a web or form project?

User's cannot enter any data into a label, so I don't see why they would fire validation.

cbp
this question is totally confused, and i think that it is winapp
MoreThanChaos
A: 

Take look for "text changed" event, i think you missunderstand using of validation in case of controls.

In method which handles "text changed" event for tb1 you can put code which while editing change value in tb2 to be complementary to 100 and vice versa editing tb2 will cause automatic change in tb1. You can also put in this event handlers reaction for improper values

take care

MoreThanChaos
I know there are 50 ways to do this. I am using the built-in Validation in the CLR. Thus, I have validation on each entry control, such as text boxes. In order to leverage this, you place code in the Validating event. This is not "wrong", and "text changed" is the wrong place to do what I desire.
Pittsburgh DBA
Accordingly, I want to place validation logic on a label that is adjacent to the two controls of which I wrote. However, despite having all of the Validation-related properties and events, the Validating event does not fire on labels, even when calling Me.ValidateChildren() on the form.
Pittsburgh DBA
A: 

According to the documentation for CausesValidation:

"Gets or sets a value indicating whether the control causes validation to be performed on any controls that require validation when it receives focus."

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

I really think you are using the wrong methodology to obtain the results you are looking for. I think it would be better to have the validators in the textbox call a utility method that will set the label accordingly.

Alarion
+2  A: 

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

Steven A. Lowe