I have a Tab Control with multiple Tab Pages which each contain multiple controls.
When a user moves away from my tab using a button, I want to validate that the controls on that tab were modified correctly.
A better way is to use the Error Provider component, which shows the user that there's a problem and provides a tooltip with extra information, as shown in Figure 1.
Figure 1: Example use of the Error Provider component When the user attempts to chance focus out of the empty text box, we use an instance of the Error Provider component to set an error associated with that text box, causing the icon to be displayed to the right of the text box and making the tool tip available if they need more info.
This behavior was achieved by dragging an Error Provider component onto the dialog and handling the Validating event like so: Notice the call to Error Provider.
Set Error, passing in the control the error is associated with (which we get from the sender argument to the Validating event), along with the error string, which will be used as the tool tip.
This article was originally published in the "Chris Sells on . I don't mean don't trust them to pay (which is a completely separate issue that I won't go into here). For that, you need to handle a control's Validating event: The Validating will be called when moving focus from a control on the dialog that has the Causes Validation property set to true to another control that has the Causes Validation property set to true, e.g. The Validating event gives the handler the chance to cancel the move of focus by setting the Cancel Event Args. In this example, if the user doesn't enter a name into the text box, than the Validating event handler notifies the user of their transgression and cancels the event, which will keep the focus on the text box with invalid data.
First, add the following code to the Form’s constructor after the Initialize Component method: This is a handy trick to prevent implicit validation of our controls when they lose focus.
In this case, we simply clear any error messages in the error provider.
The Name textbox works very similarly but we don’t have quite as elaborate validation logic.
If the user enters invalid data they’ll see an icon shown in Figure 3.
After the validating event returns without being cancelled, meaning we have valid user input, then the validated event will be raised.