|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Validating all controls on "Save"I have a windows form that I want to force validation on controls (text
boxes) when the user clicks a "Save" button. The only way I've found to do this is to cycle through every control and call it's .Select() method. This is clunky though because you can see a flash in each text box as it's being validated. Here's my code Private Sub Save() For each c as control in Me.Controls If c.CanSelect() then c.Select() End if Next c End Sub Each control has code in their Control_Validating event that fires off an errorprovider.
Show quote
Hide quote
"Ryan" <Tyveil@newsgroups.nospam> wrote in message
news:OHMN0vCrGHA.4960@TK2MSFTNGP04.phx.gbl... > I have a windows form that I want to force validation on controls (text > boxes) when the user clicks a "Save" button. The only way I've found to do > this is to cycle through every control and call it's .Select() method. This > is clunky though because you can see a flash in each text box as it's being > validated. Here's my code > > Private Sub Save() > For each c as control in Me.Controls > If c.CanSelect() then > c.Select() > End if > Next c > End Sub > > Each control has code in their Control_Validating event that fires off an > errorprovider. > >
Show quote
Hide quote
"Ryan" <Tyveil@newsgroups.nospam> wrote in message Try using the LockWindowUpdate API call to prevent the objects from beingnews:OHMN0vCrGHA.4960@TK2MSFTNGP04.phx.gbl... > I have a windows form that I want to force validation on controls (text > boxes) when the user clicks a "Save" button. The only way I've found to do > this is to cycle through every control and call it's .Select() method. This > is clunky though because you can see a flash in each text box as it's being > validated. Here's my code > > Private Sub Save() > For each c as control in Me.Controls > If c.CanSelect() then > c.Select() > End if > Next c > End Sub > > Each control has code in their Control_Validating event that fires off an > errorprovider. updated while you cycle through the list. This will prevent the "flash" effect. If you hit an error, or you successfully reach the end of the list, a call to LockWindowUpdate(0&) releases the lock. Since only one lock can be in place at any one time you would want to lock the whole form during the validation phase. (It doesn't cost anything extra.) Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long) As Long LockWindowUpdate(Me.Handle) ' Locks the form if "Me" is the form LockWindowUpdate(0&) ' Unlocks whatever was locked previously It doesn't stop the user from entering text but the added (or changed or removed) text won't appear until after the unlock. Hi Ryan,
Thank you for posting. Have you set the CausesValidation property of the Save button to true? If yes, when the focus is moved from one of these textboxes to the Save button, the textbox's Validating event will be raised. In this case, you needn't cycle through every control and call its Select method to force validation. However, if the user doesn't edit in any textbox on the form and click the Save button directly, the textbox's Validating event won't be raised. In this case, I think the simplest solution is to validate these textboxes in the Save button's Click event handler, instead of validating the textboxes in their Validating event handler. Of course, you could include the code of validating these textboxes in a function and call this function in the Save button's Click event handler. Hope this helps. If you have anything unclear, please feel free to let me know. Sincerely, Linda Liu Microsoft Online Community Support ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. Ryan,
Why are you not adding the click event of your save button to all the validating methods. You can probably even do that in one time using the same routine you have now, but use the validating method and add that handler to that (what I did not try) Cor Show quoteHide quote "Ryan" <Tyveil@newsgroups.nospam> schreef in bericht news:OHMN0vCrGHA.4960@TK2MSFTNGP04.phx.gbl... >I have a windows form that I want to force validation on controls (text >boxes) when the user clicks a "Save" button. The only way I've found to do >this is to cycle through every control and call it's .Select() method. >This is clunky though because you can see a flash in each text box as it's >being validated. Here's my code > > Private Sub Save() > For each c as control in Me.Controls > If c.CanSelect() then > c.Select() > End if > Next c > End Sub > > Each control has code in their Control_Validating event that fires off an > errorprovider. > Doh,
That probably won't go, the events have different event arguments. But you can of course set in your Save Click event a routine that calls all the validating methods you want. Cor Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schreef in bericht news:uLP8KVJrGHA.4684@TK2MSFTNGP05.phx.gbl... > Ryan, > > Why are you not adding the click event of your save button to all the > validating methods. > > You can probably even do that in one time using the same routine you have > now, but use the validating method and add that handler to that (what I > did not try) > > Cor > > > > "Ryan" <Tyveil@newsgroups.nospam> schreef in bericht > news:OHMN0vCrGHA.4960@TK2MSFTNGP04.phx.gbl... >>I have a windows form that I want to force validation on controls (text >>boxes) when the user clicks a "Save" button. The only way I've found to >>do this is to cycle through every control and call it's .Select() method. >>This is clunky though because you can see a flash in each text box as it's >>being validated. Here's my code >> >> Private Sub Save() >> For each c as control in Me.Controls >> If c.CanSelect() then >> c.Select() >> End if >> Next c >> End Sub >> >> Each control has code in their Control_Validating event that fires off an >> errorprovider. >> > > Ryan,
Are you using VS 2005 or an earlier version. With VS 2005 you can use something like: Private Sub buttonAccept_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonAccept.Click If Not ValidateChildren() Then DialogResult = Windows.Forms.DialogResult.None Return End If End Sub Where you have AutoValidate set on the form itself: Me.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange And you have DialogResult set on the OK button itself. buttonOk.DialogResult = System.Windows.Forms.DialogResult.OK For earlier versions I use a loop similar to yours, however I use Control.Focus & Form.Validate. Private Sub buttonAccept_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonAccept.Click For Each control As control In Me.Controls If control.CausesValidation Then control.Focus() If Not Me.Validate() Then Me.DialogResult = DialogResult.None Exit For End If End If Next End Sub NOTE: This loop probably should be recursive to get controls within container controls, within other container controls... The above routine was adopted from Chris Sells' book "Windows Forms Programming in C#" from Addison Wesley. The "DialogResult = Windows.Forms.DialogResult.None" above prevents the dialog box from closing & returning DialogResult.OK! -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "Ryan" <Tyveil@newsgroups.nospam> wrote in message news:OHMN0vCrGHA.4960@TK2MSFTNGP04.phx.gbl... |I have a windows form that I want to force validation on controls (text | boxes) when the user clicks a "Save" button. The only way I've found to do | this is to cycle through every control and call it's .Select() method. This | is clunky though because you can see a flash in each text box as it's being | validated. Here's my code | | Private Sub Save() | For each c as control in Me.Controls | If c.CanSelect() then | c.Select() | End if | Next c | End Sub | | Each control has code in their Control_Validating event that fires off an | errorprovider. | |
Diagnostics.Process & MSIEXEC problem...
How to Return Value from Module? exceptions/inner exceptions Getting the full path of a folder. Referencing a MDI Parent form from a Child Expose Count from System.Collections.CollectionBase in an inherited class Force Windows to "See" new files burned to a CD/DVD? Obtain info from other computer Assembly info from separate project? Beginner VB.NET group? |
|||||||||||||||||||||||