New to Telerik UI for Xamarin? Download free 30-day trial

Events

The RadMaskedInput exposes the following events which can be utilized for additional control of the user's input:

  • ApplyMaskStarted: Occurs when you type a symbol, before the mask is applied.
  • ApplyMaskFinished: Occurs after you type a symbol, when the mask is already applied.

ApplyMaskStarted

The event occurs when you type in a symbol in the RadMaskedInput and the mask is about to be applied. The event arguments are of type ApplyMaskStartedEventArgs and expose following properties:

  • NewDisplayedText(string): The updated text of the MaskedInput control after the symbol is added.
  • NewInputValue(string): The new InputValue(the whole string value that the mask contains).
  • OldDisplayedText(string): The text of the MaskedInput before the symbol is added.
  • OldInputValue(string): The previous input value. Returns empty string if such is not available.

ApplyMaskFinished

The event occurs when the mask is already applied. The event arguments are of type ApplyMaskFinishedEventArgs and expose the following properties:

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

Example

ApplyMaskStarted example

Here is a sample definition of the RadMaskedInput control

<StackLayout Margin="16">
    <Label Text="The default C token allows x to be input. Using event we reject x from input."/>
    <telerikInput:RadMaskedInput x:Name="input" Mask="CC (00)" Placeholder="#" />
</StackLayout>

In addition to this you will need to add the following namespace:

xmlns:telerikInput="clr-namespace:Telerik.XamarinForms.Input;assembly=Telerik.XamarinForms.Input"

then attach the event handler to the MaskedInput control:

this.input.ApplyMaskStarted += Input_BeforeTextMasked;

and here is a sample implementation of the handler:

private void Input_BeforeTextMasked(object sender, Telerik.XamarinForms.Input.MaskedInput.ApplyMaskStartedEventArgs e)
{
    e.NewDisplayedText = e.NewDisplayedText.ToUpper();
}

ApplyMaskFinished example

Here is a sample definition of the RadMaskedInput control

<StackLayout Margin="16">
    <Label Text="The default C token allows x to be input. Using event we reject x from input."/>
    <telerikInput:RadMaskedInput x:Name="input" Mask="CC (00)" Placeholder="#" />
</StackLayout>

In addition to this you will need to add the following namespace:

xmlns:telerikInput="clr-namespace:Telerik.XamarinForms.Input;assembly=Telerik.XamarinForms.Input"

then attach the event handler to the MaskedInput control:

this.input.ApplyMaskFinished += Input_AfterTextMasked;

and here is a sample implementation of the handler:

private int lastValidCaretPosition = 0;
private void Input_AfterTextMasked(object sender, Telerik.XamarinForms.Input.MaskedInput.ApplyMaskFinishedEventArgs e)
{
    if (e.NewDisplayedText.Contains("X"))
    {
        e.IsAccepted = false;
        e.CaretPosition = lastValidCaretPosition;
    }
    else
    {
        lastValidCaretPosition = e.CaretPosition;
    }
}

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

See Also

In this article