

If you take some time at it you should be able to eliminate a large chunk of the code your wrote here and still have the same result. Since you are only appear to be checking if any one of the controls is not valid you should break out of the check immediately upon finding this case since there is no need to check any more controls. your validation method should simply take a control as it's argument, inside that method iterate the Controls collection of that control and pass them back into the validate method recursively. You should look at using a recursive function instead for iterating the controls and pass in the child control collection items to the recursive method, that way you do not need to make a special case for a tab control or other controls that contain child controls like panels etc, much simpler cleaner code. You have template text still showing in your article. Validate(objForm, objControl.Controls, Valid) Validate(objForm, objTab.Controls, Valid) If TypeOf objControl Is XtraTabControl Thenĭim TabControl As XtraTabControl = objControlĭim Index As Integer = TabControl.SelectedTabPageIndexįor Each objTab As XtraTabPage In TabControl.TabPages If Not objForm.Validate() Then Valid = False If Not TypeOf objControl Is RadioButton Then Private Shared Sub Validate(ByRef objForm As Form, ByRef objControls As .ControlCollection, ByRef Valid As Boolean)įor Each objControl As Control In objControls If Not objform.Validate Then Valid = False Validate(objform, TopLevelControl.Controls, Valid) Public Shared Function FormIsValid(ByRef objform As Form, ByRef TopLevelControl As Control) As Boolean If Not objForm.Validate Then Valid = False Validate(objForm, objForm.Controls, Valid) Public Shared Function FormIsValid(ByRef objForm As Form) As Boolean I like Asp.Nets method better, but with this class, you can live with it and maybe even love it. You just have to call "If Validation.FormIsValid Then." and your entire form is validated in one statement. Then once you're in the habit of doing that on each form, you can use the template below and some copy and pasting to put validators in for each control you want to validate, and then when you hit your save button. Make sure other buttons, especially the cancel button has causesvalidation=false so that they won't validate. Make sure your save button has causesvalidation = true along with any control you want to validate. Put an e.cancel = false in the FormClosing event so that the cancel button, and the x button at the top of the form works even when the form isn't validated. Turn autovalidate to "enable allow focus change" for the best user experience. I like to name mine ErrorProvider on every form so I don't have to change stuff in each code.
#Devexpress errorprovider grid windows#
I use devexpresses control set, so my special case is that tab control, but with another couple lines of code, you can make it handle any special case such as the windows tab control, possibly split containers, etc. I had to go back and add a special condition for this case. This worked great until I started including tabcontrols on forms, and then I found out that it would only validate properly for the tab page that was currently selected. If it wanted to validate after each control lost focus, then I would just loop through all the controls and give it focus, and call validate.

Net provided, and made a simple class to take advantage of how it wanted to work. ValidateChildren seemed like a viable solution, but it didn't validate everything for me either. The problem with me.validate() is that is only validates the last focused control.

You need to validate that the information was entered and it was entered correctly.

I have a grid, and then you open up a form to add/edit an object. I have a general method that is probably similar to everyone elses. All you want is just one method that allows you to validate your entire form at once before you submit. Are you frustrated with the error-provider.
