Commands

Apart from utilizing the Events that the control exposes for modeling the user input, you can also achieve this by creating custom commands.

When creating custom commands, you should inherit the MaskedInputCommandBase class and explicitly set the ID of your custom command to one of the available within the MaskedInputCommandID enum.

Custom ApplyMaskStarted Command

In case you would like to apply some logic through a command when the ApplyMaskStarted event is called, you need to set the ID of your custom command to MaskedInputcommandId.ApplyMaskStarted. Eventually, the parameter that you will receive in the Execute method will be of type ApplyMaskStartedCommandContext. It exposes the following properties, which are identical to those of the ApplyMaskStartedEventArgs:

  • NewDisplayedText: The updated text of the MaskedInput control after the symbol is added.
  • NewInputValue: The symbol that was typed.
  • OldDisplayedText: The text of the MaskedInput before the symbol is added.
  • OldInputValue: The previous input value. Returns empty string if such is not available.

Here is an example of a custom command that is invoked on ApplyMaskStarted:

public class CustomBeforeTextMaskedCommand : MaskedInputCommandBase
{
    public CustomBeforeTextMaskedCommand()
    {
        this.Id = MaskedInputCommandId.ApplyMaskStarted;
    }

    public override void Execute(object parameter)
    {
        var context = parameter as ApplyMaskStartedCommandContext;
        context.NewDisplayedText = context.NewDisplayedText.ToUpper();
    }
}

You should also make sure that the command is added in the Commands collection of the RadMaskedInput instance:

this.input.Commands.Add(new CustomBeforeTextMaskedCommand());

Custom ApplyMaskFinished Command

Similar to the ApplyMaskStarted Command, you can create a custom command that is executed when the ApplyMaskFinished event occurs. In this case, you need to set the ID of your custom command to MaskedInputcommandId.ApplyMaskFinished The parameter that you will receive in the Execute method will then be of type ApplyMaskFinishedCommandContext. Identically to ApplyMaskFinishedEventArgs, it has the following properties:

  • CaretPosition: The position of the caret after the symbol was typed. It can be controlled to manually set the caret position.
  • IsAccepted: Boolean property that indicates whether the symbol was accepted by the mask. It can be set to manually control the action.
  • NewDisplayedText: The updated text of the MaskedInput control after the symbol is added.
  • NewInputValue: The symbol that was typed.
  • OldDisplayedText: The text of the MaskedInput before the symbol is added.
  • OldInputValue: The previous input value. Returns empty string if such is not available.

Here is an example of a custom command that is invoked on ApplyMaskFinished:

public class CustomAfterTextMaskedCommand : MaskedInputCommandBase
{
    public CustomAfterTextMaskedCommand()
    {
        this.Id = MaskedInputCommandId.ApplyMaskFinished;
    }

    private int lastValidCaretPosition = 0;
    public override void Execute(object parameter)
    {
        var context = parameter as ApplyMaskFinishedCommandContext;
        if (context.NewDisplayedText.Contains("X"))
        {
            context.IsAccepted = false;
            context.CaretPosition = lastValidCaretPosition;
        }
        else
        {
            lastValidCaretPosition = context.CaretPosition;
        }
    }
}

Once again, make sure that the custom command is added to the Commands collection of the RadMaskedInput contro:

this.input.Commands.Add(new CustomAfterTextMaskedCommand());

You can find a working demo in the MaskedInput/Features/Commands folder of the SDK Samples Browser application.

See Also