Team LiB
Previous Section Next Section

Testing the Masked Edit Control

Testing the new control is simple. Start a new Windows Forms project in C# or VB. Mine is called "MaskClient." Note here that I can use the VB control in the C# project or vice versa. This is the CLR at work.

You will need to add the control to your Toolbox. You can make a new Toolbox tab or use one of the existing ones. I use the General tab. Bring up the General tab on the Toolbox. Right-click the tab and choose Customize Toolbox. Choose the .NET Framework Components tab in the Customize Toolbox dialog box.

If you are writing in VB, click Browse and look for your DLL in the BIN subdirectory of your project. Select it and click OK on the dialog box. If you are writing in C# you will find the DLL in the BIN\Debug subdirectory of your project. Figure 8-7 shows the dialog box after you have chosen the control.

Click To expand
Figure 8-7: Choosing the new control

Figure 8-8 shows what your Toolbox should now look like.


Figure 8-8: The new control in the Toolbox

You have a single gear to work with. You can change this icon if you want. See the online help for assistance.

Drag this control over to the form and call it me1. Drag another control such as a TextBox over to the form as well. This other control allows you to leave the Masked Edit control and catch the Validate event. Your form should look like the one shown in Figure 8-9.

Click To expand
Figure 8-9: The test form

You can see that this new control looks the same as the TextBox. Click the Masked Edit control and look at the properties. You should see the two new properties at the bottom of the list. Click the Format property and the enumeration will appear (see Figure 8-10).

Click To expand
Figure 8-10: The new properties

If you choose one of the formats and click in the mask, you will see the correct mask appear for that format. You will also see the correct text appear in the box. This is pretty cool!

Choose None for a format and enter the following mask property: ##?/#?/##. This means allow two numbers, one alpha, a literal, a number, an alpha, a literal, and two numbers. Run the program and your form should look like Figure 8-11.

Click To expand
Figure 8-11: The running form

Enter in some characters in the edit box and you will see that only those corresponding to the mask are allowed. You will also see the control skip over the literals. Try deleting and backspacing over characters. Pretty neat, huh? Now tab out to the next control. You will not know that this control failed.

Double-click the form and add the following code.

C#

    private void Form1_Load(object sender, System.EventArgs e)
    {
      me1.ValidationError += new ValidationErrorEventHandler(MaskValid);
    }

    private void MaskValid(object sender, ValidationErrorEventArgs e)
    {
      MessageBox.Show(e.Mask + " " + e.Text);
    }

VB

  Private Sub Form1_Load(ByVal sender As System.Object, _
                         ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler me1.ValidationError, _
               New ValidationErrorEventHandler(AddressOf Valid)
  End Sub

  Private Sub Valid(ByVal sender As Object, ByVal e As ValidationErrorEventArgs)
    MessageBox.Show(e.Mask + " " + e.Text)
  End Sub

Now run the form again, enter some values in the mask control, and tab over to the next control. You will need to make sure that you reference the namespace of the control you created. Figure 8-12 shows the results.

Click To expand
Figure 8-12: Mask results

When you tab out of the control without the correct values in it, ValidateErrorEvent gets fired. If the control was valid, you would not get this event. Now this event gives you the opportunity to restore focus to this control. It allows you to be strict and lock up the whole form unless the control has the correct values.

That's it for this Masked Edit control and test program. What do you think? Pretty neat, isn't it? I can see doing this for quite a few different controls. I can see making controls that are target specific, such as a control that handles only e-mail addresses. Perhaps you want a control that's used only for login purposes. You could make the mask accept some obtuse set of characters in a certain order. You could make this login control write to a special file whenever someone tries to hack into your program. You could make this login control shut down and reformat the hard drive if three unsuccessful attempts at logging in were detected. You could

There is also another way to do this kind of thing. You could go the same route as the ErrorProvider control. You could make a control that extends properties of any control you want.


Team LiB
Previous Section Next Section