I was practicing programming in a WinForms project, so a question arose: How to apply a phone mask to a TextBox . So the first thing that came to mind was to use the KeyPress event of the component.
The logic of the code was simple, below ...
private void cbTelefone_KeyPress(object sender, KeyPressEventArgs e)
{
string numero = Convert.ToString(cbTelefone.Text);
if (numero.Length <= 10) // Telefone Fixo com DDD
{
string numeroTelefoneFixo = Convert.ToString(cbTelefone.Text);
cbTelefone.Text = String.Format(@"{0:(00)0000-0000}", numeroTelefoneFixo);
}
else if (numero.Length == 11) // Celular com DDD
{
string numeroTelefoneCelular = Convert.ToString(cbTelefone.Text);
cbTelefone.Text = String.Format(@"{0:(00)00000-0000}", numeroTelefoneCelular);
}
The TextBox is not receiving the phone mask. What should I do to fix it?