Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline
Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

Create Self-Validating Controls (Continued)

You need to validate both as the characters are entered and when all the text has been entered, because the user might enter a negative number. You need a regular expression that considers "-" valid if you want to allow negative integers because that is the first character entered when typing a negative number such as -125. Nevertheless, you cannot have a final regular expression that considers the "-" character a valid number.

These regular expression patterns validate integer input (negative and positive whole numbers as well as 0):

string patIntegerFull = "^-?[0-9]+$";
string patIntegerPartial = "^-?[0-9]*$";

These patterns are similar, but have a key difference. The partial pattern matches "-" and "-125", whereas the final matches "-125" but not "-".

Take a look at what these expressions represent (see Table 1). You can translate the validation patterns into plain English once you define the regular expression elements. The partial pattern matches strings that meet three criteria. First, the first character is a single minus sign, unless there isn't a minus sign, in which case the expression goes onto the next condition. Second, the next set of characters is a series of digits, and they can appear zero or more times. Third, you reach the end of the string. The final pattern is similar, with the key difference that the digits must appear one or more times in the second condition.

ADVERTISEMENT

Build the Control
Once you understand how these conditions work, you can begin building the controls. Object inheritance provides the easiest way to override the behavior of a .NET Windows Forms Control. You will also need to employ some attributes from the System.ComponentModel namespace to make your control friendly to the Visual Studio .NET designer (see Figure 2).

Begin by defining a base ValidatingTextBox class, which handles the validation logic as well as the undo logic when a user enters invalid characters. First, create a Class Library project in Visual Studio .NET, then add a reference to System.Windows.Forms. Next, add a new class named ValidatingTextBox to the project. You don't need all the features provided in the "Inherited User Control" scenario, so using the Add New Class wizard will suffice. You need to edit the class for your particular circumstances:

public abstract class ValidatingTextBox 
   : TextBox
{
   public event CancelEventHandler ValidationFailed;
   public event EventHandler ValueChanged;

   public ValidatingTextBox()
   {
      this.TextChanged += new 
      EventHandler(ValidatingTextBox_TextChanged);
   }
   private void 
      ValidatingTextBox_TextChanged(object sender, EventArgs e)
   {
      //...
   }
}

Note that the code omits the namespace and using statements for the sake of brevity.

The ValidatingTextBox class is an abstract class. This enables you to prevent the control from being used directly; the control is designed to be used solely as a foundation for the specialized controls such as the NumericalTextBox.

This code includes everything you need to drive the validation process. Remember: The built-in validation scheme doesn't quite accomplish everything you need the control to do. Instead of trying to edge out the built-in scheme and use its events, the ValidatingTextBox class defines two similar but different events. The ValidationFailed event is an optional event that consumers of the control can use to override the validation behavior. It's fired when a user enters invalid characters. The ValueChanged event replaces the TextChanged event and fires when a user enters (partially) valid characters.

Now you can take advantage of the partial and full regular expressions to validate the characters as they're entered (see Listing 1). The listing omits the details of most of the implementation of this class, but it covers the most important method: the internal TextChanged event handler.

Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home