Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Windows Forms
 Walkthrough: Working with the Maske...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Windows Forms Programming 
Walkthrough: Working with the MaskedTextBox Control 

Tasks illustrated in this walkthrough include:

For a full working version of the MaskedTextBox control that demonstrates advanced features such as validation using custom data types, see MaskedTextBox Control Sample.

To add a MaskedTextBox control to your form

  1. Open the form on which you want to place the MaskedTextBox control.

  2. Drag a MaskedTextBox control from the Toolbox to your form.

  3. Right-click the control and choose Properties. In the Properties window, select the Mask property and click the ... (ellipsis) button next to the property name.

  4. In the Input Mask dialog box, select the Short Date mask and click OK.

  5. In the Properties window set the BeepOnError property to true. This property causes a short beep to sound every time the user attempts to input a character that violates the mask definition.

Add a balloon tip for rejected mask input

  1. Return to the Toolbox and add a ToolTip to your form.

  2. Create an event handler for the MaskInputRejected event that raises the ToolTip when an input error occurs. The balloon tip remains visible for five seconds, or until the user clicks it.

    C#
    public void Form1_Load(Object sender, EventArgs e) 
    {
        ... // Other initialization code
        maskedTextBox1.Mask = "00/00/0000";
        maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected)
    }
    
    void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
    {
        toolTip1.ToolTipTitle = "Invalid Input";
        toolTip1.Show("We're sorry, but only digits (0-9) are allowed in dates.", maskedTextBox1, maskedTextBox1.Location, 5000);
    }
    

    Visual Basic
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.ToolTip1.IsBalloon = True
        Me.MaskedTextBox1.Mask = "00/00/0000"
    End Sub
    
    Private Sub MaskedTextBox1_MaskInputRejected(sender as Object, e as MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
        ToolTip1.ToolTipTitle = "Invalid Input"
        ToolTip1.Show("We're sorry, but only digits (0-9) are allowed in dates.", MaskedTextBox1, 5000)
    End Sub
    

Add a balloon tip for invalid data types

  1. In your form's Load event handler, assign a Type object representing the DateTime type to the MaskedTextBox control's ValidatingType property:

    C#
    private void Form1_Load(Object sender, EventArgs e)
    {
        // Other code
        maskedTextBox1.ValidatingType = typeof(System.DateTime);
        maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
    }
    

    Visual Basic
    Private Sub Form1_Load(sender as Object, e as EventArgs)
        // Other code
        MaskedTextBox1.ValidatingType = GetType(System.DateTime)
    End Sub
    
  2. Add an event handler for the TypeValidationCompleted event:

    C#
    public void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
    {
        if (!e.IsValidInput)
        {
           toolTip1.ToolTipTitle = "Invalid Date Value";
           toolTip1.Show("We're sorry, but the value you entered is not a valid date. Please change the value.", maskedTextBox1, 5000);
           e.Cancel = true;
        }
    }
    

    Visual Basic
    Public Sub MaskedTextBox1_TypeValidationCompleted(sender as Object, e as TypeValidationEventArgs)
        If Not e.IsValidInput Then
           ToolTip1.ToolTipTitle = "Invalid Date Value"
           ToolTip1.Show("We're sorry, but the value you entered is not a valid date. Please change the value.", maskedTextBox1, 5000)
           e.Cancel = True
        End If
    End Sub
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker