CharacterCasing on MaskedTextBox

Editors for WPF Forum

Posted 10 years ago by David Mullin
Avatar
I wanted to add the equivalent of TextBox.CharacterCasing to a subclass of MaskedTextBox. I realize that I could do this in the Mask property, but for internal reasons, we'd like to handle it through this property.

However, I can't seem to find any place to intercept the text and force it to upper case. I tried trapping the TextChanged event, and put in a body like:

if (string.IsNullOrEmpty(Text) == false)
{
    string newText = Text;
    if (CharacterCasing == CharacterCasing.Upper)
    {
        newText = newText.ToUpper();
    }
    else if (CharacterCasing == CharacterCasing.Lower)
    {
        newText = newText.ToLower();
    }
    if (Text != newText)
    {
        Text = newText;
    }
}
but, while the code ran, the Text of the control never changed. Is there some better event to trap? Is there some other way to do this?

Comments (4)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi David,

This isn't something that can easily be done using just events. You could accomplish it by deriving from the MaskedTextBox and overriding the ReplaceText method.

I did update the MaskedTextBox so that you handle the TextChanging event and alter the text there. I've also added a QuickStart that shows how to do this, and this will be in the next maintenance release.


Actipro Software Support

Posted 10 years ago by David Mullin
Avatar
Excellent! I knew that there had to be an easy way. I overrode ReplaceText and it works as promised. I still needed to trap the TextChanged event so that, when the value is set from outside, the CharacterCasing is honored, but that's just fine.
Posted 9 years ago by Serdar Metin
Avatar
Hi.
I want to use TextChanging event. But Because of the fact that i have to write my code suitable for mvvm pattern, i want to take my code to view model class. How can i do it?

Thanks for your help
Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Serdar,

In the future, please ask separate questions as a new post or support ticket. This is also a general MVVM question, which you may be better served by asking on StackOverflow or another forum that discusses MVVM.

But to answer your question, you should be able to hook up to the event in your ViewModel, as it should be able to attach to the views various events.


Actipro Software Support

The latest build of this product (v2019.1 build 0681) was released 10 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.