MaskedTextBox.TypeValidationCompleted 事件

定義

發生於 MaskedTextBox 完成了目前使用 ValidatingType 屬性的值之剖析作業後。

public:
 event System::Windows::Forms::TypeValidationEventHandler ^ TypeValidationCompleted;
public event System.Windows.Forms.TypeValidationEventHandler TypeValidationCompleted;
public event System.Windows.Forms.TypeValidationEventHandler? TypeValidationCompleted;
member this.TypeValidationCompleted : System.Windows.Forms.TypeValidationEventHandler 
Public Custom Event TypeValidationCompleted As TypeValidationEventHandler 

事件類型

範例

下列程式碼範例會嘗試將使用者的輸入剖析為有效的 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

備註

控制項 MaskedTextBox 會選擇性地針對其 MaskedTextBox.ValidatingType 屬性所定義的類型驗證使用者輸入。 當這個屬性不是 null 時,會發生下列一系列的事件:

  1. 當發生下列其中一項時,驗證順序就會開始:

  2. 任何這些事件都會導致呼叫 Parse 使用 ValidatingType 屬性指定之型別的 方法。 Parse 負責將格式化的輸入字串轉換成目標型別。 成功的轉換相當於成功的驗證。

  3. 傳回之後 Parse ,就會 TypeValidationCompleted 引發 事件。 此事件的事件處理常式最常實作,以執行類型或遮罩驗證處理。 它會接收 TypeValidationEventArgs 包含轉換相關資訊的參數;例如, IsValidInput 成員會指出轉換是否成功。

  4. 在事件的事件處理常式 TypeValidationCompleted 傳回之後,會引發標準驗證事件 Validating 。 您可以實作處理常式來執行標準驗證,或許包括取消事件。

  5. 如果步驟 3 中未取消事件,則會引發標準控制項驗證事件 Validated

如果在事件處理常式中將 Cancel 屬性設定 true 為 ,則會取消事件,而且 MaskedTextBox 控制項會保留焦點,除非後續 Validating 事件將屬性的版本 CancelEventArgs.Cancel 設定回 falseTypeValidationCompleted

適用於

另請參閱