mouse v tab movement

thirteentwenty

Well-known member
Joined
Oct 9, 2008
Messages
80
Location
Honolulu
Programming Experience
Beginner
Hello again everyone , I'm working on providing error control for a few forms and have realized that depending on the method that is used to go to the next control it sometimes works and sometimes does not. Say for me, I've been using the tab key and the error control works as expected, but if I use the mouse things change, the events that I have setup up for Leave, don't always work.

Is there a "sure fire" way of getting the Leave and Enter events to work with both the mouse and the Tab?

I did some searching around here and found this post about using the _validated/_validating events.

Edit: Unlike the poster of the above link, I'm not concerned with the order of things, I just need to be sure that the fields are all filled in and that the enter/leave events have happened.

Would this be what I need to do to ensure that the events have happened?

Please advise...

And as always your guidance and assistance is greatly appreciated.
 
You can for example use ValidateChildren function method for the form or a container, which will cause the Validating event to occur for each control that has CausesValidation set to True. It can if needed be filtered even more by setting ValidationConstraints flags as parameter. This function returns True/False if all controls goes through or not. To notify user use an ErrorProvider component, in the Validating event you can set an appropriate error message for the control in question, and in Validated event you can remove that message. ErrorProvider show an error icon next to each control that fails, and the error message you provided as tooltip, which should give the user sufficent cues as to what actions to take to validate all input.
 
You can make sure all textboxes have been filled in with data like this:


VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim nogood As Boolean
        Dim ctname As String = ""
        For Each ctl As Control In Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Text = "" Then
                    nogood = True
                    ctl.Focus()
                    ctname = ctl.Name.ToString
                End If
            End If
        Next
        If nogood = True Then
            MessageBox.Show("Please complete data.", ctname)
        Else
            MessageBox.Show("All textbox data completed", "OK")
        End If
    End Sub

Each textbox would need separate code for validating the type of required data.

You can use the Leave event instead of the button click event as shown.
 
Thank you both for your replies,

@JohnH, I wasn't really sure where to start with that, being a novice your suggestion was a bit complicated but I managed to muddle through something that kind of worked... I'll be looking into the validating/validated stuff more.

@Solitare, the problem was that if I went through the motions of filling out the form with the tab key it the on leave and on enters would work fine, but if I used the mouse there were a couple of functions that were called on leave that weren't working, although I did use your solution on several other forms, and let me tell you, it kicks butt!!! no more 15 line long if statements...

How the issue was solved (sort of):
In the end the form contained too much information anyways so about half of the controls were eliminated including the few that I was having an issue with. So in the end it didn't matter...

again thank you for your solutions
 
Back
Top