Performing Data Validation in Controls

  • If a control contains a data that you think invalid, you can use an error provider to indicate what the error is.
  • Following are the control events and properties you use in data validation

  • Validation 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 problem.
  • Validated Event- Occurs when the control is done 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; other wise false. The default is True.
  • You can give an error provider’s message here
  • Example

  • We are adding one text box controls in a form and validating the text box
  • Make CausesValidation property true (By default it is true)
  • Then Add an error provider object, ErrorProvider1 from the tool box (Shown in Fig. 1) and use the SetError method of this object to display an error provider icon next to the TextBox1 with the required message.
  • Execute the Program (Shown in Fig, 2 and Fig 3)
Public Class Form3
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
'Checking whether the entry is made in the text box
If TextBox1.Text.Length = 0 Then
' set the error for an entry to be made
ErrorProvider1.SetError(TextBox1, "Hello This box can not be blank please fill in")
'Clear the error for a valid entry made
ErrorProvider1.SetError(TextBox1, "")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TextBox1.Text.Length = 0 Then
MessageBox.Show("Text Box is Empty")
MessageBox.Show("You entered " & TextBox1.Text)
End If
End Sub
End Class

Types of Validation<< Previous

Next >>Mouse Event

Our aim is to provide information to the knowledge

comments powered by Disqus