How to get the unmasked value of a MaskedTextBox?

7

I'm using a MaskedTextBox for document formatting.

It is working perfectly, but how do I get the value typed in the field without the mask being present?

    
asked by anonymous 22.04.2014 / 21:54

3 answers

9

Solution

If for example a date entered in MaskedTextBox with value '01 / 01/1991 'and you want to get only 01011991 would be the best way to do so, even serving this for any type independent of the mask.

Code:

maskedTextBox1.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals; // tira a formatação
label1.Text = maskedTextBox1.Text; //texto não formatado
maskedTextBox1.TextMaskFormat = MaskFormat.IncludePromptAndLiterals; // retorna a formatação

Image:

OnesuggestionwouldbetouseExtensiveMethods:

Like:Createafilewiththiscode,justlikeit.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceSystem.Windows.Forms{publicstaticclassMethods{publicstaticstringTextNoFormatting(thisMaskedTextBox_mask){_mask.TextMaskFormat=MaskFormat.ExcludePromptAndLiterals;StringretString=_mask.Text;_mask.TextMaskFormat=MaskFormat.IncludePromptAndLiterals;returnretString;}}}

Usage:

label1.Text=maskedTextBox1.TextNoFormatting();

Noticethattheencodinghasbeencleaned.

References:

22.04.2014 / 22:29
1

In place of the text, you can use replace indicating the character to be taken and what will be rewritten in it, in this case nothing ("").

Example of a zip code field:

    mtxtbCep.Text.Replace("-","");

Or:

    mtxtbCep.Text.Replace("-",String.Empty);
    
22.04.2014 / 22:03
0

The solution I know for this type of problem is using Replace or Remove

string data = "22/04/2014";
data = data.Replace("/", string.Empty); /* parâmetros: Caractere antigo e o substituto dele, no caso uma string vazia. */
data = data.Remove(2, 1).Remove(4,1); /* parâmetros: Índice e a quantidade de caracteres que quero remover a partir daquele Índice */
    
22.04.2014 / 22:09