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或将消息附加到多行TextBoxTo 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名为MaskedTextBox1To 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


MaskInputRejected 是的默认事件MaskedTextBox类。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发生,该字符串的任何部分将出现在MaskedTextBoxIf a string was assigned to the control that causes MaskInputRejected to occur, no part of the string will appear in MaskedTextBox.

处理默认MaskInputRejected将播放提示音声音 ifBeepOnError属性设置为trueThe 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.