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?
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?
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:
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:
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);
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 */