Question Bypass validating event for exit button

audio_uphoria

Active member
Joined
Jan 28, 2009
Messages
31
Programming Experience
Beginner
Hi, I've been racking my brain over this all morning. Basically, I have a validating event for a txtbox called txtname. If the user tries to leave txtname for another txt box without entering their name a messagebox comes up giving an error. I have an exit button which will when clicked ask you if you really want to leave then you can choose yes or no. I can't seem to figure out how to allow the user to click the exit button while leaving the txtname blank so it would bypass the error message and only bring up the exit prompt. Here's what I have coded for the validating event and the exit event. Any help GREATLY appreciated. Thanks!!

VB.NET:
Private Sub txtName_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtName.Validating
        If IsNumeric(txtName.Text) Then
            MessageBox.Show("Please enter only your first and last name", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtName.Focus()

        ElseIf txtName.Text = "" Then
            MessageBox.Show("Please enter your name in name field", "Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            txtName.Focus()


        End If

    End Sub


VB.NET:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Dim name As String
        Name = txtName.Text
        ' When clicking the exit button, message box will ask user to confirm exit
        IsExiting = True
        If txtName.Text = "" Then
            If MessageBox.Show("Would you like to end this program?", "End Program?", MessageBoxButtons.OKCancel, _
        MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                Me.Close()
            End If
        End If
        If txtName.Text <> "" Then
            If MessageBox.Show(name & ", would you like to end this program?", "End Program?", MessageBoxButtons.OKCancel, _
    MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
                Me.Close()
            End If
        End If

    End Sub
 
Last edited:
All you had to do was read the documentation for the Validating event:
If the CausesValidation property is set to false, the Validating and Validated events are suppressed.
That's the CausesValidation of the control you're moving TO, not the one you're moving FROM.
 
Back
Top