MaskedTextBox.ValidatingType 属性

定义

获取或设置用于验证用户输入数据的数据类型。

public:
 property Type ^ ValidatingType { Type ^ get(); void set(Type ^ value); };
[System.ComponentModel.Browsable(false)]
public Type ValidatingType { get; set; }
[System.ComponentModel.Browsable(false)]
public Type? ValidatingType { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.ValidatingType : Type with get, set
Public Property ValidatingType As Type

属性值

表示 Type 验证中使用的数据类型。 默认值为 null

属性

示例

下面的代码示例尝试将用户的输入分析为有效的 DateTime。 如果失败,事件处理程序向用户 TypeValidationCompleted 显示错误消息。 如果值有效 DateTime,则代码将执行附加检查,以确保提供的日期不在今天的日期之前。 此代码示例要求 Windows 窗体项目包含一个名为MaskedTextBoxMaskedTextBox1的控件和ToolTip一个名为的ToolTip1控件。

private void Form1_Load(object sender, EventArgs e)
{
    maskedTextBox1.Mask = "00/00/0000";
    maskedTextBox1.ValidatingType = typeof(System.DateTime);
    maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
    maskedTextBox1.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);

    toolTip1.IsBalloon = true;
}

void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
    if (!e.IsValidInput)
    {
        toolTip1.ToolTipTitle = "Invalid Date";
        toolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", maskedTextBox1, 0, -20, 5000);
    }
    else
    {
        //Now that the type has passed basic type validation, enforce more specific type rules.
        DateTime userDate = (DateTime)e.ReturnValue;
        if (userDate < DateTime.Now)
        {
            toolTip1.ToolTipTitle = "Invalid Date";
            toolTip1.Show("The date in this field must be greater than today's date.", maskedTextBox1, 0, -20, 5000);
            e.Cancel = true;
        }
    }
}

// Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires.
void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    toolTip1.Hide(maskedTextBox1);
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.MaskedTextBox1.Mask = "00/00/0000"
    Me.MaskedTextBox1.ValidatingType = GetType(System.DateTime)

    Me.ToolTip1.IsBalloon = True
End Sub

Private Sub MaskedTextBox1_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles MaskedTextBox1.TypeValidationCompleted
    If (Not e.IsValidInput) Then
        Me.ToolTip1.ToolTipTitle = "Invalid Date"
        Me.ToolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", Me.MaskedTextBox1, 0, -20, 5000)
    Else
        ' Now that the type has passed basic type validation, enforce more specific type rules.
        Dim UserDate As DateTime = CDate(e.ReturnValue)
        If (UserDate < DateTime.Now) Then
            Me.ToolTip1.ToolTipTitle = "Invalid Date"
            Me.ToolTip1.Show("The date in this field must be greater than today's date.", Me.MaskedTextBox1, 0, -20, 5000)
            e.Cancel = True
        End If
    End If
End Sub

' Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires.
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MaskedTextBox1.KeyDown
    Me.ToolTip1.Hide(Me.MaskedTextBox1)
End Sub

注解

掩码本身不保证用户的输入将表示给定类型的有效值。 以下 C# 代码显示掩码:

maskedTextBox1.Mask = "99/99/9999";

以下Visual Basic代码显示掩码:

MaskedTextBox1.Mask = "99/99/9999"

此掩码可以要求用户输入八位数,但无法验证用户是否在正确的范围内输入月份、日期和年份值;“2003/12/20”和“70/90/0000”在掩码方面同样有效。

可用于 ValidatingType 验证用户输入的数据是否位于正确的范围内 - 在前面提到的案例中,通过为其分配类型的实例 DateTime 。 当用户离开控件时,控件中的当前文本都将得到验证。 可以通过监视 TypeValidationCompleted 事件来确定数据是否通过验证失败。 MaskedTextBox将仅对ValidatingType是否MaskCompletedtrue执行检查。

如果要使用自己的自定义数据类型, ValidatingType则必须实现一个静态 Parse 方法,该方法采用字符串作为参数。 必须使用以下一个或两个签名来实现此方法:

public static Object Parse(string)

public static Object Parse(string, IFormatProvider)

适用于

另请参阅