|
|||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionInput controls such as the BackgroundThis scheme was originally devised for a project that had a large number of diverse checks to be performed on input data. The display was dynamically generated making it preferable to separate the graphical control from the validation. Using the sample applicationThe sample project shows three text input fields. Each of these has a validator implementation attached to it. If the correct value is entered, the background stays white. If an incorrect value is entered the background goes red and a message box gives some details of the error. In this sample the user is not forced to correct the error by keeping focus on the control that is in error. Field 1 is a regex check. In the sample, we check for a valid hex representation. Fields 2 and 3 are interrelated. The value in field 2 is a decimal value that must lie within the range specified by field 3. Field 3 specifies a range where the high value must be greater than the low value. Description of the codeWe use two interfaces here. //
// IValidate Interface
//
public interface IValidate
{
string Text{get;set;}
bool IsValid{get;}
IDisplay TheDisplay{get;set};
}
The //
// The IDisplay Interface
//
public interface IDisplay
{
string baseText{get;set;}
Control TheControl{get;}
}
Let's take a look at the code in the /// <SUMMARY>
/// Set the validator for this control
/// </SUMMARY>
public IValidate TheValidator
{
set
{
theValidator = value;
theValidator.TheDisplay = this;
this.CausesValidation = true;
this.Validating += new CancelEventHandler(validatedTextBox_Validating);
}
get{return theValidator;}
}
The public attribute /// <summary>
/// Validation event handler. Invokes the validator if present
/// </summary>
/// <PARAM name="sender"></PARAM>
/// <PARAM name="e"></PARAM>
private void validatedTextBox_Validating(object sender, CancelEventArgs e)
{
if (null != theValidator)
{
theValidator.Text = this.Text;
}
}
The event handler First a look at the base validator class. The public string Text
{
get {return textValue;}
set
{
textValue = value;
validate();
updateDisplay();
}
}
Next let's look at the actual implementation of a validation and what that does in relation to our scheme. public override bool validate()
{
Regex aRegex = new Regex(theRegex);
IsValid = aRegex.IsMatch(Text);
return IsValid;
}
This Next comes the question of what to do when the input text is not what it should be. The method used here is to put up a message box and set the background color of the control. Looking again at the public override void updateDisplay()
{
base.updateDisplay ();
if (!IsValid)
{
System.Windows.Forms.MessageBox.Show("Incorrect input" +
" for regular expression: " + theRegex);
}
}
First the common processing is performed by calling public virtual void updateDisplay()
{
if (null != TheDisplay)
{
TheDisplay.baseText = Text;
if (IsValid)
{
TheDisplay.TheControl.BackColor = System.Drawing.Color.White;
}
else
{
TheDisplay.TheControl.BackColor = System.Drawing.Color.Salmon;
}
}
}
The base class method ConclusionWe have presented a scheme that separates the implementation of validation from the control. The validator provides feedback into the control by giving visual feedback and by modifying the text. The control itself can be used with or without a supporting validator class.
|
||||||||||||||||||||||||||||||||||||||||||||