Question how to bypass a leave event

audio_uphoria

Active member
Joined
Jan 28, 2009
Messages
31
Programming Experience
Beginner
Hi, I'm programming something and I have it set to where if you leave the text box blank then a message box comes up and tells you to enter a value to move on to the next text box
VB.NET:
Private Sub txtName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Leave
        If IsNumeric(txtName.Text) Then
            MessageBox.Show("Please enter only your first and last name", "Error")
            txtName.Focus()
        ElseIf txtName.Text = "" Then
            MessageBox.Show("Please enter your name in name field", "Error")
            txtName.Focus()
       elseif 


        End If
    End Sub

I also have the exit button programmed as follows

VB.NET:
 Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click


        ' When clicking the exit button, message box will ask user to confirm exit
        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 Sub

Now whats currently happening is if I click the exit button and leave the name text box blank the error i programmed will come up and tell me to enter a name. I want to be able to click the exit button while no text is entered in the name text box bypassing the error message. How would I do that? Thanks!
 
I'm so close....

VB.NET:
Private Sub txtName_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.Leave
        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()
        ElseIf FrmMain.btnExit_Click() Then
            Me.Close()




        End If
    End Sub

what do i put in the parentheses after btnexit_click????
 
You're going about this all wrong I'm afraid. First up, have you read the documentation for the Leave event? If you have then you'll know that there is also a Validating event. THAT is the event that you use to validate the contents of a control, NOT the Leave event.

As for the last part, how can you put the Click event handler of a Button in an ElseIf statement when that event handler doesn't return a value? btnExit_Click would have to return a Boolean for it to be able to be used like that. How can you test IF something is True when it can't possibly be True or False.

Finally, I really can't see why you would be prompting the user to exit the application when they leave a TextBox. Is that really what you want to do? Assuming it is, these are the steps you should be implementing:

1. Handle the Validating event of the TextBox. In that event handler, if the contents of the control is not valid simply set e.Cancel to True. This will prevent the Validated and Leave events being raised and the control will not lose focus.

2. Handle the Leave event of the TextBox. If the Leave event is raised you know the control's contents are valid so no validation is required. All you need to do is call the PerformClick method of the Button. NOTHING else.

3. Handle the Click event of the Button exactly as you are.

There are other considerations here too. As I said, the TextBox will not lose focus if the contents are not valid. This will prevent the user clicking the Button themselves without entering a name, even if they don't want to continue. That is generally a bad thing.
 
Alright, first of all I want to thank you. You have been a great help to me. I had no idea of the validating event.... My instructor told us to program the checks (if txtname = "" then; if isnumeric (txtname.text) then... etc...)

Ok so I moved the checks to the validating event and assigned btnexit.perform click to the txt name leave event. Well now if I try to leave the box empty and click the exit button the message saying I need to enter a valid entry comes up and then the exit prompt comes up. The only thing I need to figure out is how to stop the invalid entry error from coming up thus allowing the exit prompt to come up.
 
Last edited:
When you try to move focus from one control to another, by default the control you're moving from will be validated. If there are certain controls you want the user to be able to move to without validating the previous control, you need to set the CausesValidation property of those controls to False. This is usually done for Cancel buttons, allowing the user to cancel changes without having to enter valid data they will never save. You'd also do it for Help buttons and the like.
 
Back
Top