We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Changing mask based on first character


I'm trying to implement certain functionality with the MaskedTextBox but I'm running into issues. I was hoping that somebody might have some insight.

What I want is for the mask to change based on the first character in the MaskedTextBox. Initially the MaskedTextBox is empty and the mask is empty but if the user were to type in 1, the mask would be "C-0000" if the user typed in 2 instead, then it would be "C-LLLL".

I've tried changing the mask in the event handler for TextChanged and ValueChanged but this seems to push the cursor back to the beginning of the text.

Another issue is that if I use 1 first then highlight all and press 2, the mask is still "C-0000".

The relevant code is:

<sync:MaskedTextBox Height="25"

        private void MaskedEditor_ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            if (MaskedEditor.Value[0] == '1')
                MaskedEditor.Mask = "C-0000";
            else if (MaskedEditor.Value[0] == '2')
                MaskedEditor.Mask = "C-LLLL";

Any help is appreciated. Thank you.

5 Replies

VR Vijayalakshmi Roopkumar Syncfusion Team March 23, 2015 08:15 PM UTC

Hi James,

Thank you for contacting the Syncfusion Support,

We have prepared a sample that tries to meet your requirement. In our sample we have handled the provided code in the PreviewKeyDown event, so that the cursor will be placed correctly and also when selected the 1 in the MaskedTextBox , the second value Mask property does not remain the same as the first value. Please download the sample using the below link:


Please let us know if you have any other concerns,

Vijayalakshmi V.R.

JA James March 24, 2015 05:00 PM UTC

Thanks for this sample Vijayalakshmi.

I'm working with it right now and it looks like with your help I will be able to achieve what I need.


SC Saranya CJ Syncfusion Team March 25, 2015 05:43 AM UTC

Hi James,

Thank you for your update. Please let us know if you require any other assistance on this.



PA Paul Alexandre JANSSENS July 19, 2023 02:18 PM UTC


I have a similar issue. 

I have a SfMaskedEdit (phone number)

The mask is declared in code behind when page is loaded. 

I put a TextChanged event to change the mask in function of the two first digits.

The mask is not changing and caret is going in the middle of the box.

In fact, the mask sould be : 

 "\\([0]\\d{0}\\)[1-9]\\d{1}\\/ [0-9]\\d{1}\\.[0-9]\\d{1}\\.[0-9]\\d{1}";

If the two first digits are '02' OR '03' OR '04'

The mask have to be :

\\([0]\\d{0}\\)[1-9]\\d{0}\\/ [0-9]\\d{2}\\.[0-9]\\d{1}\\.[0-9]\\d{1}"

Have y

VN Vignesh Nagarajan Syncfusion Team July 20, 2023 01:41 PM UTC

Hi Paul Alexandre JANSSENS,


We are able to update the mask at run time using TextChanged event. Please refer to the attached sample and video for your reference.


Could you please share the following details?

  • Expected behavior regarding the caret position
  • Video demonstrating the issue.
  • Code snippets of XAML and C#
  • If possible, please share modified sample




Attachment: Sample_and_Output_7b2e45ee.zip

Live Chat Icon For mobile
Up arrow icon