473,378 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 4718

"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.