How to cancel a form with a text box with the Leave Event coded
-
My New User form has a pair of text boxes used to define new users to my app. For both I've used the 'Leave' event to validate them before the form's OK button is enabled. (I've included the code for the simpler one of the two below.) I also want a Cancel button so that the user can choose not to set up a new user. At the mo, the Cancel button just sets DialogResult.Cancel. When the user clicks on the Cancel button the first time, the Leave event is still triggered and the users gets a "You must enter a name." message box. If the user clicks Cancel again, the window closes. I realise that I could do the checks when the user clicks OK but it seems 'more OO' to validate each box. How can I avoid this message box on cancel but still check the formatting when the user moves out of the text box otherwise?
Private Sub txtNewOppName_Leave(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtNewOppName.LeaveIf txtNewOppName.Text = "" Then Me.NameDefined = False MessageBox.Show("You must enter a name.") Me.Select() Else Me.NameDefined = True End If If Me.EmailDefined And Me.NameDefined Then Me.btnOk.Enabled = True End If
End Sub