Events in .NET MAUI Masked Entry

21 Jul 20268 minutes to read

This section provides information about the events available in the .NET MAUI Masked Entry control. Events are typically subscribed in the page constructor or in code-behind, and are raised on the UI thread.

The following events are available in the Masked Entry control:

  1. ValueChanging - raised before the value is committed, and is cancelable.
  2. ValueChanged - raised after the value is committed.
  3. Completed - raised when the user finalizes text by pressing the return key.
  4. ClearButtonClicked - raised when the user taps the clear button.

Prerequisites

Before using the SfMaskedEntry, ensure the following NuGet package is installed in your .NET MAUI project:

  • Syncfusion.Maui.Inputs

For a step-by-step setup, refer to the Getting Started documentation.

ValueChanging Event

The ValueChanging event is raised when the Value property is about to change in the Masked Entry control, for example on each keystroke that modifies the value. The event handler receives a MaskedEntryValueChangingEventArgs instance that provides the following properties:

  • NewValue: Gets the newly entered value that is about to be committed.
  • OldValue: Gets the previous value before the change.
  • IsValid: Gets a boolean value indicating whether the input is considered valid based on the mask completion.
  • Cancel: Gets or sets a value indicating whether the change should be canceled. When set to true, the Value property is not updated.
<editors:SfMaskedEntry x:Name="maskedEntry"
                       WidthRequest="200"
                       MaskType="Simple"
                       Mask="(000) 000-0000"
                       ValueChanging="MaskedEntry_ValueChanging"/>
SfMaskedEntry maskedEntry = new SfMaskedEntry
{
    WidthRequest = 200,
    MaskType = MaskedEntryMaskType.Simple,
    Mask = "(000) 000-0000"
};
maskedEntry.ValueChanging += MaskedEntry_ValueChanging;

The ValueChanging event can be handled in C# as follows:

private void MaskedEntry_ValueChanging(object sender, MaskedEntryValueChangingEventArgs e)
{
    // Access the new and old values
    string newValue = e.NewValue?.ToString();
    string oldValue = e.OldValue?.ToString();

    // Check the validity of the input
    bool isValid = e.IsValid;

    // Cancel the event if the new value is not allowed
    if (!e.IsValid)
    {
        e.Cancel = true;
    }
}

ValueChanged Event

The ValueChanged event is raised after the Value property is changed in the Masked Entry control. The event handler receives a MaskedEntryValueChangedEventArgs instance that provides the following properties:

  • IsMaskCompleted: Gets a boolean value indicating whether all the required inputs for the mask are completed.
  • NewValue: Gets the newly committed value.
  • OldValue: Gets the previous value before the change.
<editors:SfMaskedEntry x:Name="maskedEntry"
                       WidthRequest="200"
                       MaskType="Simple"
                       Mask="(000) 000-0000"
                       ValueChanged="MaskedEntry_ValueChanged"/>
SfMaskedEntry maskedEntry = new SfMaskedEntry
{
    WidthRequest = 200,
    MaskType = MaskedEntryMaskType.Simple,
    Mask = "(000) 000-0000"
};
maskedEntry.ValueChanged += MaskedEntry_ValueChanged;

The ValueChanged event can be handled in C# as follows:

private void MaskedEntry_ValueChanged(object sender, MaskedEntryValueChangedEventArgs e)
{
    // Access the new and old values
    string maskNewValue = e.NewValue?.ToString();
    string maskOldValue = e.OldValue?.ToString();
    if (e.IsMaskCompleted)
    {
        // Handle the case when the mask is completed
    }
}

Completed Event

The Completed event is raised when the user finalizes the text in the Masked Entry editable mode by pressing the return key on the keyboard. The handler for the event is a generic EventHandler, taking the sender and EventArgs (the EventArgs value is EventArgs.Empty):

<editors:SfMaskedEntry x:Name="maskedEntry"
                       WidthRequest="200"
                       MaskType="Simple"
                       Mask="(000) 000-0000"
                       Completed="MaskedEntry_Completed"/>
SfMaskedEntry maskedEntry = new SfMaskedEntry
{
    WidthRequest = 200,
    MaskType = MaskedEntryMaskType.Simple,
    Mask = "(000) 000-0000"
};
maskedEntry.Completed += MaskedEntry_Completed;

The Completed event can be handled in C# as follows:

private void MaskedEntry_Completed(object sender, EventArgs e)
{
    // Place this handler in the code-behind of a ContentPage,
    // or resolve the current page via Application.Current?.Windows[0]?.Page.
    var page = Application.Current?.Windows[0]?.Page;
    page?.DisplayAlert("Message", "Text entering Completed", "ok");
}

ClearButtonClicked Event

The ClearButtonClicked event is raised when the user activates the clear button in the Masked Entry editable mode by tapping the clear button. The handler for the event is a generic EventHandler, taking the sender and EventArgs.

<editors:SfMaskedEntry x:Name="maskedEntry"
                       WidthRequest="200"
                       MaskType="Simple"
                       Mask="(000) 000-0000"
                       ClearButtonVisibility="WhileEditing"
                       ClearButtonClicked="MaskedEntry_ClearButtonClicked"/>
SfMaskedEntry maskedEntry = new SfMaskedEntry
{
    WidthRequest = 200,
    MaskType = MaskedEntryMaskType.Simple,
    Mask = "(000) 000-0000",
    ClearButtonVisibility = ClearButtonVisibility.WhileEditing
};
maskedEntry.ClearButtonClicked += MaskedEntry_ClearButtonClicked;

The ClearButtonClicked event can be handled in C# as follows:

private void MaskedEntry_ClearButtonClicked(object sender, EventArgs e)
{
    // Place this handler in the code-behind of a ContentPage,
    // or resolve the current page via Application.Current?.Windows[0]?.Page.
    var page = Application.Current?.Windows[0]?.Page;
    page?.DisplayAlert("Message", "Clear Button Clicked", "ok");
}

See Also