Leave event triggers even when dialogresult = cancel

Joined
Jun 10, 2004
Messages
6
Programming Experience
Beginner
I have a form which has a couple of text boxes which are used to define new users to my app. Both use the Leave event to make sure they're filled in before the OK button is enabled. (I've included the code for the first of them below.) I also want a Cancel button so that the user can choose not to set up a new user.

When the user clicks on cancel 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 a bit nasty. How can I avoid this message box on cancel but still detect the error when the user moves out of the text box otherwise?

VB.NET:
Private Sub txtNewOppName_Leave(ByVal sender As Object, _
		 ByVal e As System.EventArgs) Handles txtNewOppName.Leave
If 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
 
Private Sub btnCancel_Click(ByVal sender As System.Object, _
		 ByVal e As System.EventArgs) Handles btnCancel.Click
Me.DialogResult = DialogResult.Cancel
End Sub
 
Last edited:
Back
Top