472,952 Members | 1,835 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,952 software developers and data experts.

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.
Jul 20 '06 #1
6 4676

"Ryan" <Ty****@newsgroups.nospamwrote in message
news:OH**************@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.


Jul 20 '06 #2

"Ryan" <Ty****@newsgroups.nospamwrote in message
news:OH**************@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.
Try using the LockWindowUpdate API call to prevent the objects from being
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.
Jul 20 '06 #3
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 21 '06 #4
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" <Ty****@newsgroups.nospamschreef in bericht
news:OH**************@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.

Jul 21 '06 #5
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

"Cor Ligthert [MVP]" <no************@planet.nlschreef in bericht
news:uL**************@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" <Ty****@newsgroups.nospamschreef in bericht
news:OH**************@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.


Jul 21 '06 #6
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.EnableAllowFocus Change

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!

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Ryan" <Ty****@newsgroups.nospamwrote in message
news:OH**************@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.
|
|
Jul 21 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dariusz | last post by:
I have the following code which executes successfully to call the browsers "save as" box... //Screensavers if ($FileType == 'scr') { // We'll be outputting a screensaver header('Content-type:...
2
by: RickL | last post by:
I have an ASP application that uploads a specified file to the server. To retrieve the file, I simply assign the filepath and file to a hyperlink on the page. When you click "Save Target As" for...
27
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I...
1
by: J. Koskey | last post by:
Background: We have hundreds of codes = specific departments, but there are frequent changes/additions to the info. For users to look up definitions, we had set up a way in Access to create a...
4
by: Richard | last post by:
Hi I'm new to ASP/Web programming so any help would be appreciated... Situation: On my web page I would like to present a link {or button} that would allow the user to download a large file. ...
5
by: Paul Sullivan | last post by:
We are a state agency that views protected medical information via our intranet. The screens even have privacy shields. Alarmingly, uses can "Print" and "Save As" which destroys the protection of...
3
by: B-Dog | last post by:
I'm checking some files to see if the filenames are in a certain format and if not I want to pull up a dialog box that gives me a save as with the file that is in question. I have all the files in...
1
by: craigslist.jg | last post by:
Hi, I've built a client side webpage for our internal users, which when loaded, generates some HTML within an iFrame. The HTML generated to the iFrame is then saved as HTML. I know it's useless...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.