![]() ![]() | ||
Now that we're creating data-entry forms, it's useful to know that you can validate the data the user enters into controls. If a control contains data that you think invalid, you can use an error provider (see the previous topic) to indicate what the error is. Here are the control events and properties you use in data validation:
Validating Event— Occurs when the control is validating, which happens when the control loses the focus (if the control's CausesValidation property is True). Place code to check the control's data here, and throw an exception if there's a problem. You can set an error provider's message here.
Validated Event— Occurs when the control is done validating. This event occurs if no exception was thrown in the Validating event. You can clear an error provider's message here.
CausesValidation Property— True if entering the control causes validation to be performed on other controls requiring validation when this control gets the focus; otherwise, False. The default is True.
You can see how this works in the Validation example on the CD-ROM. In that example, I use two text boxes and code that insists that the user enter data into each text box. If the user hasn't entered data into a text box and clicks the other text box (making the current text box lose the focus), the error provider in this example displays an icon with a tool tip indicating what the error was, as you see in Figure 21.17.
To follow along in this example, add two text boxes to a project's main form, and make sure their CausesValidation property is set to True (this is the default), which means that when either text box receives the focus, it'll cause all the other controls' Validating events to occur. Also, add an error provider object, ErrorProvider1, from the toolbox.
In the Validating event for TextBox1, I check to see if the user has entered data into that text box, and if not, use the SetError method of the error provider to display an error provider icon next to the text box, with a message, "Please enter a value", which will be displayed in the icon's tool tip, as you see in Figure 21.17. I also throw an exception so Visual Basic knows the data in the control did not validate properly:
Private Sub TextBox1_Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) Handles _ TextBox1.Validating If TextBox1.Text.Length = 0 Then ErrorProvider1.SetError(TextBox1, "Please enter a value") Throw New Exception("You must enter a value.") End If End Sub
If the text box data did not validate, the icon and tool tip appear, as you see in Figure 21.17. On the other hand, if the data does validate properly, no exception is thrown by the code in the Validating event, and the Validated event for the control occurs. I'll clear the error provider's error in this event's handler in case the user has corrected a data-entry error and the error icon is showing (this will hide the error icon if it's showing):
Private Sub TextBox1_Validated(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox1.Validated ErrorProvider1.SetError(TextBox1, "") End Sub
And I'll also add error handling for the second text box, TextBox2, in the same way:
Private Sub TextBox2_Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) Handles _ TextBox2.Validating If TextBox2.Text.Length = 0 Then ErrorProvider1.SetError(TextBox2, "Please enter a value") Throw New Exception("You must enter a value.") End If End Sub Private Sub TextBox2_Validated(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles TextBox2.Validated ErrorProvider1.SetError(TextBox2, "") End Sub
And that's it—now we're validating the data the user enters into controls, as you see in Figure 21.17.
![]() ![]() | ||