MaskedTextBox.MaskInputRejected MaskedTextBox.MaskInputRejected MaskedTextBox.MaskInputRejected MaskedTextBox.MaskInputRejected Event


当用户的输入或者分配的字符与输入掩码的对应格式元素不匹配时发生。Occurs when the user's input or assigned character does not match the corresponding format element of the input mask.

 event System::Windows::Forms::MaskInputRejectedEventHandler ^ MaskInputRejected;
public event System.Windows.Forms.MaskInputRejectedEventHandler MaskInputRejected;
member this.MaskInputRejected : System.Windows.Forms.MaskInputRejectedEventHandler 
Public Custom Event MaskInputRejected As MaskInputRejectedEventHandler 


下面的代码示例演示如何使用此成员。The following code example demonstrates the use of this member. 在此示例中, 事件处理程序报告MaskInputRejected事件的发生。In the example, an event handler reports on the occurrence of the MaskInputRejected event. 此报表可帮助你了解何时发生事件, 并可帮助你进行调试。This report helps you to learn when the event occurs and can assist you in debugging. 若要报告多个事件或频繁发生的事件, 请考虑MessageBox.ShowConsole.WriteLine消息替换为多行或将TextBox其追加到多行。To report on multiple events or on events that occur frequently, consider replacing MessageBox.Show with Console.WriteLine or appending the message to a multiline TextBox.

若要运行示例代码, 请将其粘贴到包含名为MaskedTextBox MaskedTextBox1的类型的实例的项目中。To run the example code, paste it into a project that contains an instance of type MaskedTextBox named MaskedTextBox1. 然后, 确保事件处理程序与MaskInputRejected事件相关联。Then ensure that the event handler is associated with the MaskInputRejected event.

private void MaskedTextBox1_MaskInputRejected(Object sender, MaskInputRejectedEventArgs e) {

System.Text.StringBuilder messageBoxCS = new System.Text.StringBuilder();
messageBoxCS.AppendFormat("{0} = {1}", "Position", e.Position );
messageBoxCS.AppendFormat("{0} = {1}", "RejectionHint", e.RejectionHint );
MessageBox.Show(messageBoxCS.ToString(), "MaskInputRejected Event" );
Private Sub MaskedTextBox1_MaskInputRejected(sender as Object, e as MaskInputRejectedEventArgs) _ 
     Handles MaskedTextBox1.MaskInputRejected

    Dim messageBoxVB as New System.Text.StringBuilder()
    messageBoxVB.AppendFormat("{0} = {1}", "Position", e.Position)
    messageBoxVB.AppendFormat("{0} = {1}", "RejectionHint", e.RejectionHint)
    MessageBox.Show(messageBoxVB.ToString(),"MaskInputRejected Event")

End Sub


MaskInputRejectedMaskedTextBox类的默认事件。MaskInputRejected is the default event for the MaskedTextBox class.

MaskInputRejected某个字符被输入掩码拒绝时发生此事件。The MaskInputRejected event occurs when a character is rejected by the input mask. Mask属性表示的输入掩码通过MaskedTextProvider属性与该控件关联的掩码文本提供程序来进行解释。The input mask, represented by the Mask property, is interpreted by the masked text provider associated with the control through the MaskedTextProvider property. MaskInputRejected在以下情况下会引发:MaskInputRejected is raised in the following situations:

  • 输入字符与相应的格式元素不匹配。An input character does not match the corresponding format element. 例如,用户在需要数字时输入字母字符。For example, the user enters an alphabetic character when a digit is required. 这可能是引发此事件的最常见原因。This is probably the most common reason why this event is raised.

  • 用户尝试在掩码末尾以外输入无关字符,这可能是因为已经填充了掩码,或者当前插入符号位置已移至显示的输入掩码字符串的最末尾。The user is trying to input extraneous characters beyond the end of the mask either because the mask has already been filled or the current caret position has been moved to the very end of the displayed input mask string.

  • "粘贴" 操作会插入与其关联格式元素不匹配的字符,如果IsOverwriteMode属性为false,则它会将现有字符移动到与其格式元素不匹配的新位置。A paste operation either inserts a character that does not match its associated format element, or if the IsOverwriteMode property is false, it shifts existing characters into new positions where they do not match their format elements.

  • 剪切操作将现有字符向左移动,并且一个或多个字符与新关联的格式元素不匹配。A cut operation shifts existing characters to the left, and one or more characters do not match their newly associated format elements.

  • Text属性进行了赋值,分配的字符串导致了一个或多个掩码冲突。An assignment was made to the Text property and the assigned string caused one or more mask violations.

如果向控件分配了导致MaskInputRejected出现的字符串,则不会在中MaskedTextBox显示字符串的任何部分。If a string was assigned to the control that causes MaskInputRejected to occur, no part of the string will appear in MaskedTextBox.

如果将MaskInputRejected true属性设置为,则的默认处理将发出嘟嘟声。 BeepOnErrorThe default handling for MaskInputRejected will play a beep sound if the BeepOnError property is set to true. 此事件通常用于实现自定义错误处理,例如,在掩码已满时移到下一个用户输入控件; 如果输入字符无效,则显示 "自定义错误" 对话框或工具提示。This event is often handled to implement custom error handling, for example, to move to the next user input control if the mask is full, or to display a custom error dialog box or ToolTip if the input character is invalid.