How to standardize the phone input in a Textbox C#

Asked

Viewed 42 times

-1

Hello, I’m doing a Customer Registration system and I have a problem that I have no idea how to fix, I know it’s "simple" but I’m programming a little and I haven’t found any way that really works.

To register a customer there is the phone field where I did a treatment to organize the number, without mask.

protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.BackColor = Color.White;

            string TelCel;
            TelCel = this.Text;
            string result = TelCel;
            {
                if (TelCel.Length == 13)
                {
                    this.Text = TelCel.Insert(0, "(").Insert(3, ")").Insert(9, "-");
                }
                if (TelCel.Length == 11)
                {
                    this.Text = TelCel.Insert(0, "(").Insert(3, ")").Insert(9, "-");
                }
                if (TelCel.Length == 10)
                {
                this.Text = TelCel.Insert(0, "(").Insert(3, ")").Insert(8, "-");
                }
                {
                    if ((TelCel.Length != 11) && (TelCel.Length != 10))
                    {
                        this.Text = TelCel;
                    }
                }
            }
        }

By typing the phone in his hand he’ll come out like this: inserir a descrição da imagem aqui

However when copying and pasting Whatsapp phone or other place it does not do the formatting: inserir a descrição da imagem aqui

However he enters the database in both ways, but if I want to make a query I can not due to Parentheses, I would like to know how to standardize the phone number, either in Ctrl+c, Ctrl+v or by typing the hand.

Thanks in advance for the help!

  • Is that Windows Forms? Ideally, use a mask component.

2 answers

0

Good afternoon! I would use maskedTextBox instead of textbox.

Example:

inserir a descrição da imagem aqui

Placing the following properties:

inserir a descrição da imagem aqui

Typing:

inserir a descrição da imagem aqui

Copying and Pasting:

inserir a descrição da imagem aqui

0

Thanks Marcelo and Thanks LINQ for the help, also managed to create a Textbox class specific for phones, so I can use the same control throughout my project throughout the project! I added only 3 lines within the class and managed to solve the problem, so far it worked.. kk

For those interested, follow the code, I hope I can help someone.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;

namespace Telefone.Controle
 {
class txtTelefone: TextBox
{
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.BackColor = Color.White;

            //Linhas adicionadas para apagar qualquer espaço vazio do TextBox
            string str = this.Text;
            str = Regex.Replace(str, @"\s", "");
            this.Text = str;

            string TelCel;
            TelCel= this.Text;
            string result = TelCel;
            {
                if (TelCel.Length == 12)
                {
                    this.Text = TelCel.Insert(0, "(").Insert(3, ")");
                }
                if (TelCel.Length == 13)
                {
                    this.Text = TelCel.Insert(0, "(").Insert(3, ")").Insert(9, "-");
                }
                if (TelCel.Length == 11)
                {
                    this.Text = TelCel.Insert(0, "(").Insert(3, ")").Insert(9, "-");
                }
                if (TelCel.Length == 10)
                {
                    this.Text = TelCel.Insert(0, "(").Insert(3, ")").Insert(8, "-");
                }
                {
                    if ((TelCel.Length != 11) && (TelCel.Length != 10) && (TelCel.Length != 12)) 
                    {
                        this.Text = TelCel;
                    }
                }
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Escape)
            {
                this.Text = "";
                e.SuppressKeyPress = true;
            }
        }
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            if (Char.IsDigit(e.KeyChar)) return;
            if (char.IsControl(e.KeyChar)) return;
            e.Handled = true;
        }
}
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.