Profilo di DamianDamo's spot on the webFotoBlogElenchi Strumenti Guida
17 ottobre

WinForms Validation Code

When trying out some of the new WinForms features I quickly set about seeing how validation in winforms had changed. Turns out it hasn't 
 
You see, my history is in web development and as such I have enjoyed one of best features of ASP.NET 1.1, the validation controls. When I went to create my first WinForms application I was shocked that there were no equivalent controls for doing form validation. Instead you get the ErrorProvider component (extender control even?) that you can use along with the appropriate control and form events to display a little icon next to controls that need attention. Hmmm, not quite the same development experience as ASP.NET. So I went about devising a routine for validating forms in WinForms.
 
In the end all it is is a few IsValid() methods but I think it makes it fairly simple to setup validation code and use the error provider. First of all, there is an IsValid() method that takes the control to validate and a string to store any validation error message. It returns a boolean to indicate whether the control's value is valid. The body is basically a large case statement (on the control's name) containing the validation logic for each control. Simple enough.
 
This method is used in a couple of ways. Firstly it is used by universal handlers for the controls validating events (or other events if more apppropriate) to set the error provider message on the controls. Secondly it is used by an overload of the IsValid() method that is for validating the whole form, or any group of controls really.
 
The second IsValid() method takes a Control.ControlCollection and a StringCollection to return the validation messages in. It just loops through the controlCollection and calls the first IsValid() method for each of them. Fairly simple but as I said, it results in a fairly clean and as easy to maintain set of validation functions.
 

Commenti

Attendere...
Il commento immesso è troppo lungo. Immetti un commento più breve.
Immissione non effettuata. Riprova.
Impossibile aggiungere il commento al momento. Riprova più tardi.
Per aggiungere un commento è necessaria l'autorizzazione di un genitore. Chiedi autorizzazione
I tuoi genitori hanno disattivato i commenti.
Impossibile eliminare il commento al momento. Riprova più tardi.
Hai raggiunto il numero massimo di commenti pubblicabili giornalmente. Riprova tra 24 ore.
Impossibile lasciare commenti. La funzionalità è stata disattivata perché i sistemi hanno rilevato una possibile attività di spamming dal tuo account. Se ritieni che il tuo account è stato disattivato per errore, contatta il supporto tecnico di Windows Live.
Esegui il seguente controllo di protezione per completare la pubblicazione del commento.
I caratteri digitati nel controllo di protezione devono corrispondere ai caratteri dell'immagine o della riproduzione audio.
Damian Edwards ha disattivato i commenti di questa pagina.

Riferimenti

L'URL di riferimento per questo intervento è:
http://damianpedwards.spaces.live.com/blog/cns!A079DE667E1958B3!183.trak
Blog che fanno riferimento a questo intervento
  • Nessuno