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 Forms專案包含 MaskedTextBox 名為 MaskedTextBox1 的控制項,以及 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"

此遮罩可以要求使用者輸入八位數,但無法確認使用者輸入正確範圍內的月份、日期和年份值;「12/20/2003」 和 「70/90/0000」 與遮罩所考慮的程度相同。

您可以使用 ValidatingType 來驗證使用者輸入的資料是否落在正確的範圍內,在先前提到的案例中,藉由指派類型的實例 DateTime 。 當使用者離開控制項時,將會驗證控制項中的目前文字。 您可以藉由監視 TypeValidationCompleted 事件來判斷資料是否失敗驗證。 MaskedTextBox只有在 是 true 時,才會對 ValidatingTypeMaskCompleted 執行檢查。

如果您想要搭配 使用自己的自訂資料類型 ValidatingType ,您必須實作採用字串做為參數的靜態 Parse 方法。 這個方法必須實作下列一或兩個簽章:

public static Object Parse(string)

public static Object Parse(string, IFormatProvider)

適用於

另請參閱