Αντί να βάζεις τά Events τών Controls στην Validated δοκίμασε να τά βάλεις στην Validating και τότε θα δείς ότι πρώτα εκτελείται το event σου και κατόπιν γίνεται Close της φόρμας και άν έχεις περάσει στην System.ComponentModel.CancelEventArgs τιμή Cancel=true δέν εκτελείται το close event της φόρμας.
Η validated εκτελείται αφού έχει εκτελεστεί η Validating π.χ.(από το HELP του VS)
Private Sub MyValidatingCode(ByVal sender As Object)
' Confirm there is text in the control.
If sender.Text.Length = 0 Then
Throw New Exception("Email address is a required field")
Else
' Confirm that there is a "." and an "@" in the e-mail address.
If sender.Text.IndexOf(".") = -1 Or TextBox1.Text.IndexOf("@") = -1 Then
Throw New Exception("E-mail address must be valid e-mail address format." + _
ControlChars.Cr + "For example '[email protected]'")
End If
End If
End Sub
Private Sub textBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating, TextBox2.Validating
Try
MyValidatingCode(sender)
Catch ex As Exception
' Cancel the event and select the text to be corrected by the user.
e.Cancel =
True
sender.Select(0, sender.Text.Length)
' Set the ErrorProvider error with the text to display.
Me.ErrorProvider1.SetError(sender, ex.Message)
End Try
End Sub
Private Sub textBox1_Validated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TextBox1.Validated, TextBox2.Validated
' If all conditions have been met, clear the error provider of errors.
ErrorProvider1.SetError(sender, "")
End Sub
Ιωάννης Μανουσάκης