我有自动格式化电话号码的代码,格式是 12 3456-7890
1234567890 = 12 3456-7890 (TextLength = 12)
我想要如果 TextLength = 13 以这种方式格式化
12345678901 = 12 34567-8901 (TextLength = 12) 或者换句话说,将“-”的位置更改为向右 1 个位置并在最后一个字符上添加最后一个数字
我的实际代码
private void txtFonecom_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Back)
{
}
else
{
if (txtFonecom.TextLength == 2)
{
txtFonecom.Text = txtFonecom.Text + " ";
txtFonecom.SelectionStart = 3;
txtFonecom.SelectionLength = 0;
}
if (txtFonecom.TextLength == 7)
{
txtFonecom.Text = txtFonecom.Text + "-";
txtFonecom.SelectionStart = 8;
txtFonecom.SelectionLength = 0;
}
if (txtFonecom.TextLength == 13)
{
//here i have to change format from 12 3456-7890 to 12 34567-8901
}
}
}