我有一個Windows窗體,其數據綁定文本框顯示的電話號碼格式如下:(800)555-5555。數據以十進制形式存儲,然後以正確的格式顯示。但問題是,當我點擊文本框,然後點擊其他東西時,它會從(800)555-5555變回8005555555.格式會丟失。我試着在textBox離開事件中再次對數字進行重新格式化,但這不起作用。什麼可能導致這個?焦點變化時TextBox格式化丟失
VS 2010的C#
格式化首先我做這個
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;
}
那麼我這樣做
private void FormatPhoneNum()
{
decimal iPhone = decimal.Parse(CustomerPhone1Box.Text);
CustomerPhone1Box.Text = string.Format("{0:(###) ###-####}", iPhone);
}
Winforms? ASP? WPF? – rerun
Winforms VS 2010 c# – John
你如何強制文本框顯示正確的格式?你可以發佈你的一些代碼嗎? –