none
Dynamically Formating TextBox

    Question

  • Any one have any good links on how i can format a textbox for a phone number in this format using .Net 1.1 like (000)-000-0000 by using the textchanged event??......I know someone has had to have done this before....help
    Tuesday, September 12, 2006 2:46 AM

Answers

  • I found a function that worked i just set the malength property of my text control to 10 digits...


    public string formatPhone(string x)
    {
    double y = 0;
    if (x.Length <= 8 || x.Length > 13)
    {
    MessageBox.Show("Your phone number does not contain the correct " +
    "number of digits. Please format number " +
    "as ###-###-####","Incorrect Number Format", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
     
    return x;
    }
    else

    {
    if (x.StartsWith("1-"))
    {
    x = x.Remove(0,1);
    x = x.Remove(0,1);
    x = x.Remove(3,1);
    x = x.Remove(6,1);
    }
    if (x.Length == 14)
    {
    x = x.Remove(0,1);
    x = x.Remove(3,1);
    x = x.Remove(3,1);
    x = x.Remove(6,1);
    y = Double.Parse(x);
    }
    else if (x.Length == 12)
    {
    x = x.Remove(3,1);
    x = x.Remove(6,1);
    y = Double.Parse(x);
    }
    else if (x.Length == 10)
    {
    y = Double.Parse(x);
    }
    else

    {
    y = Double.Parse(x);
    }
     
    x = String.Format("{0:###-###-####}", y);
     
    return x;
    }
    }

     

     

    string x = textBox1.Text;

    textBox1.Text = formatPhone(x);

    Tuesday, September 12, 2006 3:07 AM