TheBugSlayer
Programmer
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:
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.
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.