| Previous | Home | Next |
This control provides a user interface that indicates to user that a control on a form has error associated with it. In other had it works as validation upon the controls to handle the error causes by inputting wrong by user.
Drag this control as shown in below picture
Adding error provider control on textbox for blank not allow
Write this code on textbox validating event
Output will shown in below picture if user leave name blank.
Private Sub TextBox1_Validating(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
ValidateName()
End Sub
Private Function ValidateName() As Boolean
Dim bStatus As Boolean = True
If textBox1.Text = "" Then
errorProvider1.SetError(textBox1, "Please enter your Name")
bStatus = False
Else
errorProvider1.SetError(textBox1, "")
End If
Return bStatus
End Function
Validating for age. For validating age i have covered three conditions for validate
- If user leave this blank.
- If entered age is less than 18.
- If user input character value in place of numeric.
Write this code on Textbox2 validating Event
Private Sub TextBox2_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles TextBox2.TextChanged
ValidateAge()
End Sub
Private Function ValidateAge() As Boolean
Dim bStatus As Boolean = True
If textBox2.Text = "" Then
errorProvider1.SetError(textBox2, "Please enter your Age")
bStatus = False
Else
errorProvider1.SetError(textBox2, "")
Try
Dim temp As Integer = Integer.Parse(textBox2.Text)
errorProvider1.SetError(textBox2, "")
If temp < 18 Then
errorProvider1.SetError(textBox2,
"You must be atleast 18 years old to setup a test")
bStatus = False
Else
errorProvider1.SetError(textBox2, "")
End If
Catch
errorProvider1.SetError(textBox2, "Please enter your age as a number")
bStatus = False
End Try
End If
Return bStatus
End Function
Output of this error provider is shown below
Using error provider control on DatetimePicker Control
If user select weekend day for test then he will get error message- Like shown in below picture
Private Sub DateTimePicker1_Validating(ByVal sender As System.Object,
ByVal e As System.ComponentModel.CancelEventArgs)
Handles DateTimePicker1.Validating
ValidateTestDate()
End Sub
Private Function ValidateTestDate() As Boolean
Dim bStatus As Boolean = True
If (dateTimePicker1.Value.DayOfWeek = DayOfWeek.Sunday)
OrElse (dateTimePicker1.Value.DayOfWeek = DayOfWeek.Saturday) Then
errorProvider1.SetError(dateTimePicker1,
"Appointment cannot be scheduled in the weekend. Please select a weekday")
bStatus = False
Else
errorProvider1.SetError(dateTimePicker1, "")
End If
Return bStatus
End Function
Write above code on DatetimePicker Validating Event.
| Previous | Home | Next |