MaskedTextBox.ValidatingType Właściwość

Definicja

Pobiera lub ustawia typ danych używany do weryfikowania danych wejściowych przez użytkownika.

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

Wartość właściwości

Type

Reprezentujący Type typ danych używany w walidacji. Wartość domyślna to null.

Atrybuty

Przykłady

Poniższy przykład kodu próbuje przeanalizować dane wejściowe użytkownika jako prawidłową DateTimewartość . Jeśli zakończy się to niepowodzeniem, TypeValidationCompleted program obsługi zdarzeń wyświetli użytkownikowi komunikat o błędzie. Jeśli wartość jest prawidłowa DateTime, kod wykonuje dodatkowe sprawdzenie, aby upewnić się, że podany termin nie jest wcześniejszy niż dzisiejsza data. Ten przykład kodu wymaga, aby projekt Windows Forms zawierał kontrolkę MaskedTextBox o nazwie i kontrolkę ToolTip o nazwie MaskedTextBox1 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

Uwagi

Maski nie gwarantują, że dane wejściowe użytkownika będą reprezentować prawidłową wartość danego typu. Poniższy kod w języku C# przedstawia maskę:

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

Poniższy kod Visual Basic pokazuje maskę:

MaskedTextBox1.Mask = "99/99/9999"

Ta maska może wymagać wprowadzenia ośmiu cyfr przez użytkownika, ale nie może sprawdzić, czy użytkownik wprowadza wartości miesiąca, daty i roku w poprawnym zakresie; "12/20/2003" i "70/90/0000" są równie ważne, jeśli chodzi o maskę.

Możesz użyć ValidatingType polecenia , aby sprawdzić, czy dane wprowadzone przez użytkownika mieszczą się w prawidłowym zakresie — w powyższym przypadku, przypisując mu wystąpienie DateTime typu. Bieżący tekst w kontrolce zostanie zweryfikowany, gdy użytkownik opuści kontrolkę. Możesz określić, czy sprawdzanie poprawności danych kończy się niepowodzeniem TypeValidationCompleted , monitorując zdarzenie. MaskedTextBox będzie wykonywać sprawdzanie ValidatingType tylko wtedy, gdy MaskCompleted jest to true.

Jeśli chcesz użyć własnych niestandardowych typów danych z ValidatingTypeprogramem , musisz zaimplementować metodę statyczną Parse , która przyjmuje ciąg jako parametr. Ta metoda musi zostać zaimplementowana przy użyciu jednego lub obu następujących podpisów:

public static Object Parse(string)

public static Object Parse(string, IFormatProvider)

Dotyczy

Zobacz też