private void txtFone_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)8) //compara se é numero e habilita o backspace//8 valor da backspace na tabela ask { e.Handled = true; //volta verdadeiro e não faz nada ou seja não é numero } if (e.KeyChar != (char)8) { CampoT edit = CampoT.Telefone; edit = CampoT.Telefone; FormatarTelefone(edit, txtFone); } }
public void FormatarTelefone(CampoT Valor, TextBox txtTexto) { switch (Valor) { case CampoT.Telefone: txtTexto.MaxLength = 13; if (txtTexto.Text.Length == 0) { txtTexto.Text = txtTexto.Text + "("; txtTexto.SelectionStart = txtTexto.Text.Length + 1; } else if (txtTexto.Text.Length == 3) { txtTexto.Text = txtTexto.Text + ")"; txtTexto.SelectionStart = txtTexto.Text.Length + 1; } else if (txtTexto.Text.Length == 8) { txtTexto.Text = txtTexto.Text + "-"; txtTexto.SelectionStart = txtTexto.Text.Length + 1; } break; } }