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)>]
member this.ValidatingType : Type with get, set
Public Property ValidatingType As Type

プロパティ値

検証で使用されるデータ型を表す Type 。 既定値は null です。

属性

次のコード例では、ユーザーの入力を有効な DateTimeとして解析しようとします。 失敗した場合、 TypeValidationCompleted イベント ハンドラーはユーザーにエラー メッセージを表示します。 値が有効な DateTimeの場合、コードは追加のチェックを実行して、指定された日付が今日の日付より前ではないことを確認します。 このコード例では、Windows フォーム プロジェクトに、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"

このマスクでは、ユーザーが 8 桁の数字を入力することを要求できますが、ユーザーが正しい範囲で月、日付、年の値を入力することを確認することはできません。"12/20/2003" と "70/90/0000" は、マスクに関する限り同様に有効です。

ValidatingTypeを使用して、ユーザーが入力したデータが適切な範囲内にあるかどうかを確認できます。前述のケースでは、DateTime型のインスタンスを割り当てます。 ユーザーがコントロールを離れると、コントロール内の現在のテキストが検証されます。 TypeValidationCompleted イベントを監視することで、データの検証に失敗するかどうかを判断できます。 MaskedTextBoxは、MaskCompletedtrueされている場合にのみ、ValidatingTypeに対してチェックを実行します。

ValidatingTypeで独自のカスタム データ型を使用する場合は、文字列をパラメーターとして受け取る静的Parse メソッドを実装する必要があります。 このメソッドは、次のシグネチャのいずれかまたは両方を使用して実装する必要があります。

public static Object Parse(string)

public static Object Parse(string, IFormatProvider)

適用対象

こちらもご覧ください