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. #VB.NET ERRORPROVIDER 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. How do you make the error message display without having to hover your mouse over the icon? Public Class Form1 Dim errCount As Integer = 0 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DisplayErrorProvider(sender) errCount = 1 End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged DisplayErrorProvider(sender) errCount = 1 End Sub Private Sub DisplayErrorProvider(ByVal sender As Object) Select Case errCount Case 0 ErrorProvider1.SetError( _ sender, "What a dumb font!"
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |