Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking errors on controls recursively. Visible = False always?

Status
Not open for further replies.

TheBugSlayer

Programmer
Sep 22, 2002
887
US
Hello guys. I am afraid the subject line is not explicit and hope you will get past it and read the description of my problem.

I have a for with controls on it, obviously. I perform some validation and indicate an error through an ErrorProvider component. An error on the form will prevent saving data. However, since some controls contain more controls- in my case I have a tab control with three tab pages, I notice that controls withing controls have Visible = False, which yields false result and makes the code not behave as expectedm, ie allowing the form to be saved with an error. Without further ado, here is the procedure:

Code:
Public Overridable Function Any_Data_Entry_Errors(ByRef ctrls As Control.ControlCollection) As Boolean
        ' Returns True if there are any data entry errors on the page.
        Dim enumControl As Control
        Dim ctrlFirstError As Control = Nothing
        For Each enumControl In ctrls
            ' Don't check invisible or disabled controls
            If enumControl.Enabled And enumControl.Visible Then
                ' Check only controls in which data entry is possible
                If TypeOf enumControl Is TextBox Or TypeOf enumControl Is ComboBox _
                Or TypeOf enumControl Is MaskedTextBox Or TypeOf enumControl Is DateTimePicker _
                Or TypeOf enumControl Is DataGrid Or TypeOf enumControl Is DataGridView Then
                    ' Test for data entry error on control
                    If epErrorProvider.GetError(enumControl) <> String.Empty Then
                        ' If it is the first error on the form, put the focus there
                        If ctrlFirstError Is Nothing Then
                            ctrlFirstError = enumControl
                            If ctrlFirstError.CanFocus Then ctrlFirstError.Focus() _
                            Else ctrlFirstError.Select()
                        End If
                        ' In either case, signal the data entry error on the control 
                        Return True
                    End If
                End If
            End If
            ' Now, recursively test all the control's sub-controls for errors
            If Any_Data_Entry_Errors(enumControl.Controls) Then
                Return True
            End If
        Next
        Return False
    End Function

All the controls are visible as this is the default property in design. Why is the propert False when a recursive call is made? Any way around this?

Thanks.
 
This is a semi non-issue of sorts. For some reason the text fields I am interested in are set to non-visible or at least have that property set to False at execution even though its True at design. Since I don' t know where that' s taking place, I set them to True in the Load event...and it works. Now if this is a known issue please let me know. Otherwise I must take a closer look at all the inherited behavior...

Thanks!
 
Let me just warn I may be understanding something wrong, but from what you seem to be saying I would say this. If you have multiple tab pages on the current Active tab is visible. When you go through a list of controls it will go through all the controls whether they are visible or not. You may see the second tab, but that second tab page is not visible. Also a control can have focus and not be visible in some cases like with the tab pages. I hope that helps.

-I hate Microsoft!
-Forever and always forward.
 
Oh hold on right there!

What is happening is precisely that where the error is on one tab and you click save while that tab has the focus, it detects the error and doesn' r save...But when you have an error on one tab and select another tab then save, it saves! So I guess when the procedure loops through all controls on the form, visible will only be the ones in the current (selected) tab, correct?

If that's the case how do I...or should I just stop checking that the control is visible? I don' t have non-visible controls anyway so Enabled should be enough.

But if you know any other wasy please let me know...
 
So I guess when the procedure loops through all controls on the form, visible will only be the ones in the current (selected) tab, correct?"
Yes.

If I'm understanding correctly then all you really want to do is check the controls on the tab page that is currently visible? If this is correct what you want to do is iderate only through the current (Selected) tab page rather than all controls on the form.



-I hate Microsoft!
-Forever and always forward.
 
This code seems to be missing a little information and a few things seem a little redundent so I'm not quite sure all of what is going on, but you might want to change the code to something like this:
Code:
    Public Overridable Function Any_Data_Entry_Errors(ByRef ctrls As Control.ControlCollection) As Boolean
        ' Returns True if there are any data entry errors on the page.
        Dim enumControl As Control
        Dim ctrlFirstError As Control = Nothing
	Dim FirstError As Boolean = False

        For Each enumControl In ctrls
            If TypeOf enumControl is TabControl Then
		Dim cTP As TabPage = enumControl.SelectedTab
	        Dim ctrlFirstError As Control = Nothing
				
		for each ctrlFirstError in cTP.COntrols
                    If ctrlFirstError.Enabled And Then
                        ' Check only controls in which data entry is possible
                        If TypeOf ctrlFirstError Is TextBox Or TypeOf ctrlFirstError Is ComboBox _
                        Or TypeOf ctrlFirstError Is MaskedTextBox Or TypeOf ctrlFirstError Is DateTimePicker _
                        Or TypeOf ctrlFirstError Is DataGrid Or TypeOf ctrlFirstError Is DataGridView Then
                            ' Test for data entry error on control
                            If epErrorProvider.GetError(enumControl) <> String.Empty Then
                                ' If it is the first error on the form, put the focus there
                                If ctrlFirstError.CanFocus Then
				    ctrlFirstError.Focus() _
                                Else
				    ctrlFirstError.Select()
                                End If
                                ' In either case, signal the data entry error on the control 
                                Return True
                            End If
                        End If
                    End If
		Next
	    End If
            ' Now, recursively test all the control's sub-controls for errors
            If Any_Data_Entry_Errors(enumControl.Controls) Then
                Return True
            End If
        Next
        Return False
    End Function

-I hate Microsoft!
-Forever and always forward.
 
oops.

This:
Code:
If epErrorProvider.GetError(enumControl)

Should be this:
Code:
If epErrorProvider.GetError(ctrlFirstError)

-I hate Microsoft!
-Forever and always forward.
 
Thank you Sorwen for your valuable insights.

No, I do not only want to check for controls on the current tab; besides the tab there are other controls on the form...which are not on the tab.

Also, I do not restrict user movement through tabs. In other words, even if one tab has an error, the user can still move to a different one. If he forgets about the error on the other tab and tries to save from the current tab, error detection should stop him anyway.

This is an inherited procedure. It's inherited through a form template. Since don' t have any non visible controls on the form, Enabled alone is enough check. What I' ll do is override the Sub and done.

Thanks much!
 
Oh, and I will make the tab containing the error active!

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top