Bypass validating event for exit button
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!!
Code:
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
Code:
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
Re: Bypass validating event for exit button
I would keep the exit code just a confirmation dialog with a response for yes and no.
For the textbox, which sounds as though its compulsory, I would set its state to enabled and any other prohibited textboxes or controls as disabled.
When the user has entered their name, check it, disable that text box and enable the next. That way the user cannot go anywhere without putting something in the first textbox. Typically however, a user will just enter something like "dasfsfs sfsfsfs" and move on :(