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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Changing mask based on first character

Thread ID:





118587 Mar 20,2015 07:16 PM UTC Mar 25,2015 05:43 AM UTC WPF 3
Tags: MaskedTextBox
Asked On March 20, 2015 07:16 PM UTC


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.

Vijayalakshmi Roopkumar [Syncfusion]
Replied On 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.

Replied On 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.


Saranya CJ [Syncfusion]
Replied On 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.




This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon