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 프로젝트에 명명된 컨트롤과 명명 MaskedTextBoxMaskedTextBox1ToolTip 컨트롤이 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자리 숫자를 입력할 것을 요구할 수 있지만 사용자가 올바른 범위의 월, 날짜 및 연도 값을 입력했는지 확인할 수는 없습니다. "2003년 12월 20일" 및 "70/90/0000"은 마스크에 관한 한 동일하게 유효합니다.

사용자가 입력한 데이터가 형식의 인스턴스 ValidatingType 를 할당하여 이전에 언급한 경우 올바른 범위 내에 속하는지 여부를 확인하는 데 사용할 DateTime 수 있습니다. 컨트롤의 현재 텍스트는 사용자가 컨트롤을 떠날 때 유효성을 검사합니다. 이벤트를 모니터링 TypeValidationCompleted 하여 데이터가 유효성 검사에 실패하는지 여부를 확인할 수 있습니다. MaskedTextBox는 (있는 경우 ValidatingTypeMaskCompleted)에 대해서만 true 검사를 수행합니다.

사용자 고유의 사용자 지정 데이터 형식을 ValidatingType사용하려면 문자열을 매개 변수로 사용하는 정적 Parse 메서드를 구현해야 합니다. 이 메서드는 다음 서명 중 하나 또는 둘 다를 사용하여 구현해야 합니다.

public static Object Parse(string)

public static Object Parse(string, IFormatProvider)

적용 대상

추가 정보