Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

MaskedTextBox.MaskInputRejected Event

Occurs when the user's input or assigned character does not match the corresponding format element of the input mask.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public event MaskInputRejectedEventHandler MaskInputRejected
/** @event */
public void add_MaskInputRejected (MaskInputRejectedEventHandler value)

/** @event */
public void remove_MaskInputRejected (MaskInputRejectedEventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
Not applicable.

MaskInputRejected is the default event for the MaskedTextBox class.

The MaskInputRejected event occurs when a character is rejected by the input mask. The input mask, represented by the Mask property, is interpreted by the masked text provider associated with the control through the MaskedTextProvider property. 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.

  • 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.

  • An assignment was made to the Text property and the assigned string caused one or more mask violations.

If a string was assigned to the control that causes MaskInputRejected to occur, no part of the string will appear in MaskedTextBox.

The 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.

The following code example demonstrates the use of this member. 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. To report on multiple events or on events that occur frequently, consider replacing System.Windows.Forms.MessageBox.Show with System.Console.WriteLine or appending the message to a multiline TextBox.

To run the example code, paste it into a project that contains an instance of type MaskedTextBox named MaskedTextBox1. 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.AppendLine();
messageBoxCS.AppendFormat("{0} = {1}", "RejectionHint", e.RejectionHint );
messageBoxCS.AppendLine();
MessageBox.Show(messageBoxCS.ToString(), "MaskInputRejected Event" );
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

Show:
© 2015 Microsoft