7
I’m wearing a MaskedTextBox
for formatting documents.
It is working perfectly, but how do I get the value typed in the field without the mask being present?
7
I’m wearing a MaskedTextBox
for formatting documents.
It is working perfectly, but how do I get the value typed in the field without the mask being present?
10
If it is for example a date typed in MaskedTextBox
with the value '01/01/1991' and you want to catch only 01011991 would be the best way so, inclusive serve 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
Imagery:
Like: Create a file with this code, just like.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace System.Windows.Forms
{
public static class Methods
{
public static string TextNoFormatting(this MaskedTextBox _mask)
{
_mask.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
String retString = _mask.Text;
_mask.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
return retString;
}
}
}
Utilizing:
label1.Text = maskedTextBox1.TextNoFormatting();
Note that the coding has become cleaner.
References:
1
Instead of the text, you can use replace indicating the character to be taken and what will be rewritten in it, in case nothing ("").
Example of a CEP field:
mtxtbCep.Text.Replace("-","");
Or:
mtxtbCep.Text.Replace("-",String.Empty);
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 */
Browser other questions tagged c# winforms
You are not signed in. Login or sign up in order to post.
Cool! Good alternative!
– Matheus Bessa
@Matheusbessa, vlw, yours is also very good...
– user6026