Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2004

Tip: Looking for answers? Try searching our database.

form data validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan - 20 Jul 2004 21:43 GMT
I have a question about form data validation and error providers: typically,
I add some controls and an error provider to my form, then I handle the
Validating event of each control which requires validation and perform the
data check. In this handler I use the error provider SetError and set
e.Cancel to true when a validation error is found, so that the user has to
correct his input before leaving the control.
All this is fine, my question is: if there are many controls requiring
validation in the form, and the user just fills one and then clicks the OK
button without even entering the other controls, their Validating events are
*not* fired: to avoid the form closing with invalid data, I should then
re-check each control data in the OK button handler. How can I do this
without having to duplicate the validation code (once in the Validating
handler and another time in the OK button click handler)? I'd need some way
of triggering all the Validating events for each control in the form
requiring validation...

Thanx in advance!
Sijin Joseph - 22 Jul 2004 12:42 GMT
In the Ok button event handler iterate through all the controls on the form
setting focus on them and then calling Form.Validate() that will
automatically call the validating event handlers for the controls

Check out this code from Windows Form Programming in C# by Chris Sells

// Retrieve all controls and all child controls etc.
// Make sure to send controls back at lowest depth first
// so that most child controls are checked for things before
// container controls, e.g., a TextBox is checked before a
// GroupBox control
Control[] GetAllControls() {
 ArrayList allControls = new ArrayList();
 Queue queue = new Queue();
 queue.Enqueue(this.Controls);

 while( queue.Count > 0 ) {
   Control.ControlCollection
     controls = (Control.ControlCollection)queue.Dequeue();
   if( controls == null || controls.Count == 0 ) continue;

   foreach( Control control in controls ) {
     allControls.Add(control);
     queue.Enqueue(control.Controls);
   }
 }

 return (Control[])allControls.ToArray(typeof(Control));
}

void okButton_Click(object sender, EventArgs e) {
 // Validate each control manually
 foreach( Control control in GetAllControls() ) {
   // Validate this control
   control.Focus();
   if( !this.Validate() ) {
     this.DialogResult = DialogResult.None;
     break;
   }
 }
}

Signature

-Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

> I have a question about form data validation and error providers: typically,
> I add some controls and an error provider to my form, then I handle the
[quoted text clipped - 13 lines]
>
> Thanx in advance!
Dan - 22 Jul 2004 14:58 GMT
Looks like what I was looking for... Thank you very much!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.