TextBox formatting is lost when focus changes

I have a Windows form with a data-bound text box that displays a phone number generated as follows: (800) 555-5555. Data is saved as decimal and then displayed in the correct format. The problem is that when I click on the text box and then click on something else, it changes from (800) 555-5555 back to 8005555555. Formation is lost. I tried changing the numbers again on the leaveBox event, but this will not work. What could be the reason for this?

vs 2010 C #

to format first i do this

private string FormatCustPhoneBox(string a)
{
            string phone = a;

            for (int j = 0; j < phone.Length; j++)
            {
                if (!Char.IsDigit(phone, j))
                {
                    phone = phone.Remove(j, 1);  //Remove any non numeric chars.
                    j--;
                }
            }
            return phone;
}

then i do it

    private void FormatPhoneNum()
    {
        decimal iPhone = decimal.Parse(CustomerPhone1Box.Text);
        CustomerPhone1Box.Text = string.Format("{0:(###) ###-####}", iPhone);
    }
+5
source share
1 answer

? , , . .

+4

All Articles