演练:使用 MaskedTextBox 控件

本演练涉及以下任务:

创建项目并添加控件

将 MaskedTextBox 控件添加到窗体

  1. 打开要放置 MaskedTextBox 控件的窗体。

  2. 从“工具箱”将 MaskedTextBox 控件拖到窗体上

  3. 右键单击控件并选择“属性”。 在“属性”窗口中,选择“Mask”属性,然后单击属性名称旁边的“...”(省略号)按钮

  4. 在“输入掩码”对话框中,选择“短日期”掩码并单击“确定”

  5. 在“属性”窗口中,将 BeepOnError 属性设置为 true。 每次用户尝试输入违反掩码定义的字符时,此属性都会发出一声短促的提示音。

有关 Mask 属性支持的字符的摘要,请参阅 Mask 属性的“注释”部分。

提示用户存在输入错误

为被拒绝的掩码输入添加气球状提示

  1. 返回“工具箱”,将 ToolTip 添加到窗体中

  2. 为发生输入错误时引发 ToolTipMaskInputRejected 事件创建事件处理程序。 气球状提示将在五秒内或在用户单击之前保持可见状态。

    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);  
    }  
    
    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  
    

提示用户存在无效的类型

为无效数据类型添加气球状提示

  1. 在窗体的 Load 事件处理程序中,将表示 DateTime 类型的 Type 对象分配给 MaskedTextBox 控件的 ValidatingType 属性:

    private void Form1_Load(Object sender, EventArgs e)  
    {  
        // Other code  
        maskedTextBox1.ValidatingType = typeof(System.DateTime);  
        maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);  
    }  
    
    Private Sub Form1_Load(sender as Object, e as EventArgs)  
        // Other code  
        MaskedTextBox1.ValidatingType = GetType(System.DateTime)  
    End Sub  
    
  2. TypeValidationCompleted 事件添加事件处理程序:

    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;  
        }  
    }  
    
    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  
    

另请参阅