MaskedTextBox.TypeValidationCompleted MaskedTextBox.TypeValidationCompleted MaskedTextBox.TypeValidationCompleted MaskedTextBox.TypeValidationCompleted Event

定义

发生时MaskedTextBox已完成分析当前的值使用ValidatingType属性。Occurs when MaskedTextBox has finished parsing the current value using the ValidatingType property.

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对象。The following code example attempts to parse the user's input as a valid DateTime object. 如果失败,TypeValidationCompleted事件处理程序为用户显示一条错误消息。If it fails, the TypeValidationCompleted event handler displays an error message to the user. 如果值为有效DateTime,该代码验证所提供的日期不早于今天的日期。If the value is a valid DateTime, the code verifies that the date supplied is not prior to today's date. 此代码示例需要在 Windows 窗体项目,包含MaskedTextBox名为控件MaskedTextBox1和一个ToolTip控件命名为ToolTip1This code example requires that your Windows Forms project contains a MaskedTextBox control named MaskedTextBox1 and a ToolTip control named 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属性。The MaskedTextBox control will optionally validate user input against the type defined by its MaskedTextBox.ValidatingType property. 当此属性不是null,发生以下一系列事件:When this property is not null, the following series of events occurs:

  1. 验证序列开始时出现以下情况之一:The validation sequence begins when one of the following occurs:

  2. 任何这些事件会导致调用Parse与指定的类型的方法ValidatingType属性。Any of these events result in a call to the Parse method of the type specified with the ValidatingType property. Parse 负责进行格式化的输入字符串转换为目标类型。Parse is responsible for the conversion of the formatted input string to the target type. 成功转换相当于验证成功。A successful conversion equates to a successful validation.

  3. 之后Parse返回,TypeValidationCompleted引发事件。After Parse returns, the TypeValidationCompleted event is raised. 此事件的事件处理程序最常实现来执行类型或掩码验证处理。The event handler for this event is most commonly implemented to perform type or mask validation processing. 它接收TypeValidationEventArgs参数,其中包含信息有关的转换; 例如,IsValidInput成员指示转换是否成功。It receives a TypeValidationEventArgs parameter containing information about the conversion; for example, the IsValidInput member indicates whether the conversion was successful.

  4. 之后的事件处理程序TypeValidationCompleted事件返回,标准验证事件, Validating,将引发。After the event handler for the TypeValidationCompleted event returns, the standard validation event, Validating, is raised. 可以实现一个处理程序以执行标准验证,可能包括取消该事件。A handler can be implemented to perform standard validation, perhaps including canceling the event.

  5. 如果未在步骤 3 中取消事件,则标准控件的验证事件Validated引发。If the event is not canceled in step 3, the standard control validation event Validated is raised.

如果Cancel属性设置为trueTypeValidationCompleted事件处理程序,该事件将被取消并MaskedTextBox控件保留焦点,除非后续Validating事件设置其版本CancelEventArgs.Cancel属性返回到falseIf the Cancel property is set to true in the TypeValidationCompleted event handler, the event will be canceled and the MaskedTextBox control retains focus, unless the subsequent Validating event sets its version of the CancelEventArgs.Cancel property back to false.

适用于

另请参阅