Condividi tramite


MaskedTextBox.TypeValidationCompleted Evento

Definizione

Si verifica al MaskedTextBox termine dell'analisi del valore corrente tramite la ValidatingType proprietà .

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 

Tipo evento

Esempio

L'esempio di codice seguente tenta di analizzare l'input dell'utente come oggetto valido DateTime . Se ha esito negativo, il TypeValidationCompleted gestore eventi visualizza un messaggio di errore all'utente. Se il valore è un valore valido DateTime, il codice verifica che la data specificata non sia precedente alla data odierna. In questo esempio di codice è necessario che il progetto Windows Form contenga un MaskedTextBox controllo denominato MaskedTextBox1 e un ToolTip controllo denominato 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

Commenti

Il MaskedTextBox controllo convalida facoltativamente l'input dell'utente rispetto al tipo definito dalla relativa MaskedTextBox.ValidatingType proprietà. Quando questa proprietà non nullè , si verifica la serie di eventi seguente:

  1. La sequenza di convalida inizia quando si verifica una delle operazioni seguenti:

  2. Uno di questi eventi comporta una chiamata al Parse metodo del tipo specificato con la ValidatingType proprietà . Parse è responsabile della conversione della stringa di input formattata nel tipo di destinazione. Una conversione riuscita equivale a una convalida riuscita.

  3. Al Parse termine, viene generato l'evento TypeValidationCompleted . Il gestore eventi per questo evento viene generalmente implementato per eseguire l'elaborazione della convalida del tipo o della maschera. Riceve un TypeValidationEventArgs parametro contenente informazioni sulla conversione, ad esempio il IsValidInput membro indica se la conversione ha avuto esito positivo.

  4. Dopo la restituzione del gestore eventi per l'evento TypeValidationCompleted , viene generato l'evento di convalida standard, Validating, . È possibile implementare un gestore per eseguire la convalida standard, ad esempio l'annullamento dell'evento.

  5. Se l'evento non viene annullato nel passaggio 3, viene generato l'evento Validated di convalida del controllo standard.

Se la Cancel proprietà è impostata su true nel TypeValidationCompleted gestore eventi, l'evento verrà annullato e il MaskedTextBox controllo mantiene lo stato attivo, a meno che l'evento successivo Validating non imposti nuovamente la versione della CancelEventArgs.Cancel proprietà su false.

Si applica a

Vedi anche