473,587 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Validat ing event that fires off an
errorprovider.
Jul 20 '06 #1
6 4736

"Ryan" <Ty****@newsgro ups.nospamwrote in message
news:OH******** ******@TK2MSFTN GP04.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_Validat ing event that fires off an
errorprovider.


Jul 20 '06 #2

"Ryan" <Ty****@newsgro ups.nospamwrote in message
news:OH******** ******@TK2MSFTN GP04.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_Validat ing event that fires off an
errorprovider.
Try using the LockWindowUpdat e 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 LockWindowUpdat e(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 LockWindowUpdat e Lib "user32" (ByVal hwnd As Long)
As Long

LockWindowUpdat e(Me.Handle) ' Locks the form if "Me" is the form
LockWindowUpdat e(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 CausesValidatio n 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****@newsgro ups.nospamschre ef in bericht
news:OH******** ******@TK2MSFTN GP04.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_Validat ing 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.nlschre ef in bericht
news:uL******** ******@TK2MSFTN GP05.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****@newsgro ups.nospamschre ef in bericht
news:OH******** ******@TK2MSFTN GP04.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_Validat ing event that fires off an
errorprovide r.


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_Cl ick(ByVal sender As Object, ByVal e As
EventArgs) Handles buttonAccept.Cl ick
If Not ValidateChildre n() Then
DialogResult = Windows.Forms.D ialogResult.Non e
Return
End If
End Sub

Where you have AutoValidate set on the form itself:

Me.AutoValidate =
System.Windows. Forms.AutoValid ate.EnableAllow FocusChange

And you have DialogResult set on the OK button itself.

buttonOk.Dialog Result = System.Windows. Forms.DialogRes ult.OK

For earlier versions I use a loop similar to yours, however I use
Control.Focus & Form.Validate.

Private Sub buttonAccept_Cl ick(ByVal sender As Object, ByVal e As
EventArgs) Handles buttonAccept.Cl ick
For Each control As control In Me.Controls
If control.CausesV alidation Then
control.Focus()
If Not Me.Validate() Then
Me.DialogResult = DialogResult.No ne
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 "DialogResu lt = Windows.Forms.D ialogResult.Non e" 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****@newsgro ups.nospamwrote in message
news:OH******** ******@TK2MSFTN GP04.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_Validat ing 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
16337
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: application/octet-stream'); // It will be called test.scr header('Content-Disposition: attachment; filename="test.scr"');
2
2920
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 some file types, I get the following error messsage: IE can not download <file> from <server>. IE was not able to open this internet site. The requested site is either unavalable or can not be found. Can anyone tell me how to save a file of any type?...
27
5062
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 would just as soon place the employer in control of what directory he wishes to save it in, and there are two salient reasons for this: 1. I want him to OWN the document 2. I want him to FIND the document, quickly, on his hard drive In any...
1
335
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 webpage link for each separate code. Back in an earlier version of Access - 97 or possibly 95 - there was a command of <File><Save as HTML> which let us select *all* of the 350+ queries and go through a wizard to save them all as HTML. Problem:...
4
5534
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. When the user clicks to start the download I would like to display the "Save As" dialog, allow the user to name a place to save the file, and then show the download progress bar while the file downloads Question: How do I make a windows "Save...
5
1875
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 the health information at the level we want. QUESTION: Can we shut those off?? Any other suggestions?? Paul Sullivan
3
2539
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 a certain directory and if it doesn't meet my criteria then I want to do a "save as" to a different location. How can I do that. I can't seem to get the save as dialog to grab the filename of the file in question and ask for a place to save it....
1
2452
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 to disable the Save As command, but can I at least intercept when they select this from the menu and display a message? Thanks
0
8219
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.