I'm using the code below at this time. The %code% is obtained, but does not eliminate the masks to make the comparison in %code% . Anyone have any suggestions?
%pre%I'm using the code below at this time. The %code% is obtained, but does not eliminate the masks to make the comparison in %code% . Anyone have any suggestions?
%pre%You can get the value of %code% in two ways depending on what you need:
Permanent
In your MaskedTextBox change the value of the property %code% to %code% . This way, you can use %code% (anywhere) and the value returned will be the text without the mask.
Temporary
If you need the value with the mask in other parts of the code and want to remove the mask only for %code% you can do the following:
%pre%Note: None of the solutions change the visual appearance of %code% .
Example:
An example of the code you can use within the event of a button (tested in VS2013, .NET4.5):
%pre%Windows Forms
in C#
and need to get all Maskedtextbox
of the form, without the masks.
I'm using the code below at this time. The Maskedtexbox
is obtained, but does not eliminate the masks to make the comparison in if
. Anyone have any suggestions?
foreach (Control c in ctrl.Controls)
{
//Analisa os Maskedtextbox
if (c is MaskedTextBox)
{
((MaskedTextBox)c).Text.Replace("/", "").Replace(",", "").Replace("-", "");
if ((((MaskedTextBox)c).Tag == "*") && (((MaskedTextBox)c).Text.Length<=4))
{
retorno = true;
((MaskedTextBox)c).BackColor = System.Drawing.Color.FromArgb(255, 192, 192);
MessageBox.Show(c.Text);
}
else
((MaskedTextBox)c).BackColor = System.Drawing.Color.FromArgb(255, 255, 192);
}
}
You can get the value of MaskedTextBox
in two ways depending on what you need:
Permanent
In your MaskedTextBox change the value of the property TextMaskFormat
to ExcludePromptAndLiterals
. This way, you can use TextMaskFormat.Text
(anywhere) and the value returned will be the text without the mask.
Temporary
If you need the value with the mask in other parts of the code and want to remove the mask only for if
you can do the following:
maskedTextBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
var valorSemMascara = maskedTextBox.Text;
maskedTextBox.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
Note:
None of the solutions change the visual appearance of MaskedTextBox
.
Example:
An example of the code you can use within the event of a button (tested in VS2013, .NET4.5):
private void button1_Click(object sender, EventArgs e)
{
foreach (MaskedTextBox mtBox in Controls.OfType<MaskedTextBox>())
{
mtBox.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
MessageBox.Show(mtBox.Text); // Troque esta parte pelas suas condições.
mtBox.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
}
}