# VBForums CodeBank > CodeBank - C# >  SmartForm for Field Validation

## Lethal

This class extends the System.Windows.Forms.Form class by adding optional field-level validation.  To add additonal controls, simply add an overload of the Validate() method to the class, specifying the type of control. 



```
// Register your controls in the main forms constructor
this.RequiredFields = new RequiredField[] {new RequiredField(textBox1, "Entry must not be blank!")};

// To Validate the fields
if (FormIsValid()) {
    // Do whatever....
}
```



```
using System;
using System.Reflection;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;

namespace VegaSoft.WinControls {
	/// <summary>
	/// Extends the System.Windows.Forms.Form class by adding optional field-level validation.
	/// </summary>
	public class RequiredFieldBaseForm : System.Windows.Forms.Form {
		/// <summary>
		/// Private fields.
		/// </summary>
		private RequiredField[] requiredFields;
		private ErrorProvider requiredValidator = new ErrorProvider();
		/// <summary>
		/// Sets or Returns the required controls for the form.
		/// </summary> 
		public RequiredField[] RequiredFields {			
			get { return requiredFields; }
			set { 
				requiredFields = value; 
			}			
		}
		/// <summary>
		/// Sets or Returns the blink rate for the error provider.
		/// </summary> 
		public int ValidatorBlinkRate {			
			get { return requiredValidator.BlinkRate; }	
			set { 
				requiredValidator.BlinkRate = value; 
			}					
		}
		/// <summary>
		/// Sets or Returns the blink style for the error provider.
		/// </summary> 
		public ErrorBlinkStyle ValidatorBlinkStyle {
			get { return requiredValidator.BlinkStyle; }
			set { 
				requiredValidator.BlinkStyle = value; 
			}				
		}
		/// <summary>
		/// Validates the required fields and displays and sets the error
		/// providers error message if validation fails.
		/// </summary>
		/// <returns>Whether validation passed</returns>
		public bool IsFormValid() {
			bool valid = true;
			if (requiredFields != null) {
				foreach (RequiredField c in requiredFields) {
					if (ValidateField(c.Control)) {
						requiredValidator.SetIconPadding(c.Control, 2); 
						requiredValidator.SetError(c.Control, c.Message); valid = false;
					}
				}
			}
			return valid;
		}
		/// <summary>
		/// Resets all required field indicators for the controls.
		/// </summary> 
		public void ResetRequiredFieldIndicators() {
			if (requiredFields != null) {
				foreach (RequiredField c in requiredFields) {
					requiredValidator.SetError(c.Control, String.Empty);
				}
			}
		}
		/// <summary>
		/// Invokes the appropriate overload for the control being validated against.
		/// </summary>
		/// <param name="c">Control to validate</param>
		/// <returns>Whether validation passed</returns> 
		private bool ValidateField(Control c) {		
			Type[] types = {c.GetType()}; 			
			object[] parameters = {c};
			MethodInfo mi = this.GetType().GetMethod("Validate", types);			
			return (bool)mi.Invoke(this, parameters);	
		}
		/// <summary>
		/// Overload - performs the controls default validation.
		/// </summary>
		/// <param name="control">Control to validate</param>
		/// <returns>Whether validation passed</returns> 
		public bool Validate(System.Windows.Forms.TextBox control) {
			return (control.Text.Trim() == String.Empty ? true : false);
		}
		/// <summary>
		/// Overload - performs the controls default validation.
		/// </summary>
		/// <param name="control">Control to validate</param>
		/// <returns>Whether validation passed</returns> 
		public bool Validate(UltraCombo control) {
			return (control.Text.Trim() == String.Empty ? true : false);
		}
		/// <summary>
		/// Overload - performs the controls default validation.
		/// </summary>
		/// <param name="control">Control to validate</param>
		/// <returns>Whether validation passed</returns> 
		public bool Validate(UltraTextEditor control) {
			return (control.Text.Trim() == String.Empty ? true : false);
		}
		/// <summary>
		/// Overload - performs the controls default validation.
		/// </summary>
		/// <param name="control">Control to validate</param>
		/// <returns>Whether validation passed</returns> 
		public bool Validate(System.Windows.Forms.Control control) {
			return true;
		}
		/// <summary>
		/// Wrapper for a control and its associated error message when being validated.
		/// </summary>
		public struct RequiredField {
			/// <summary>
			/// Private fields.
			/// </summary>
			private string message;
			private Control control;
			/// <summary>
			/// Construct and initialize the object.
			/// </summary>
			/// <param name="control">Control to validate</param>
			/// <param name="message">Message to display when validation fails</param>
			public RequiredField(Control control, string message) {
				this.control = control;
				this.message = message;
			}	
			/// <summary>
			/// Gets the control.
			/// </summary>
			public Control Control {
				get { 
					return control; 
				}
			}	
			/// <summary>
			/// Gets the message
			/// </summary>
			public string Message {
				get {
					return message; 
				}
			}
		}
	}
}
```

----------

