Validating windows forms

I have two questions related to this: 1) Unfortunately, it seems that invoking the Validating event for the tab page does not invoke the Validating event for any of the controls on the page.Also, there does not appear to be a way to directly invoke the Validating event for each control using something like foreach (Control c in Controls)...

validating windows forms-54

Does anyone know of a nice clean way to force all the controls on a Tab Page to validate?

2) Currently, I prevent users from navigating to a particular Tab Page by handling the Selected Index Changed event.

When I clicked on another tab, the current tab's handler for the Validating event was called, and it set Cancel=true. I created a test application and utilized the Validating event with standard tabs and tabcontrols.

That worked, but as you pointed out only if one of the controls on the tabpage had focus when the tab was clicked.

Mouse Enter reacts to a mouse move over the tab, but that can't be guaranteed either (although that will probably work in 99% of the cases).

Are there any other events that would occur prior to Mouse Enter or Enter that could be used?

This might be a common scenario in data entry forms where you might create a new record and the user might not touch all the fields on your form (and thus never trigger the “Validating” event of your controls). One, pointed out by Chris Sells (read more), invokes the Control's “Notify Validating” event through Reflection.

Here is an example: You can also trigger the Validation event by giving focus to a control and taking the focus away.

This kind of control should also provide real-time feedback indicating whether the text is valid as the user enters it.

Microsoft Foundation Classes (MFC) includes these types of Text Box controls, such as the Edit control, but the MFC numerical Edit control takes the simplistic and less useful interpretation of this requirement to mean that you can enter only the characters 0 through 9 into the textbox.

Fortunately, you can address part of this deficiency yourself by building your own self-validating text input controls. NET's existing controls restrict the user input based on the type of input required.

Tags: , ,