如果使用按键,TextLength = 13,则以不同方式格式化文本

时间:2013-04-14 04:30:43

标签: c# string-formatting mask phone-number

我有自动格式化电话号码的代码, 格式为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
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我建议使用带有掩码00 0000-0000的{​​{3}}控件,而不是手动处理按键,因为控件可以自动为您输入格式。

假设您仍然喜欢使用TextBox解决方案,那么解决方案就是:

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 == 12)
        {
            int caretPos = txtFonecom.SelectionStart;
            txtFonecom.Text = txtFonecom.Text.Replace("-", string.Empty).Insert(8, "-");
            txtFonecom.SelectionStart = caretPos;
        }
    }
}

请记住,当用户删除数字时,您需要处理格式。

答案 1 :(得分:0)

此代码会将" ""-"保留在他们的位置,无论用户是删除还是在任何位置添加数字:

void txtFonecom_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (char)Keys.Back)   
            {
                if (txtFonecom.TextLength == 2)
                {
                    txtFonecom.Text = txtFonecom.Text + " ";
                    txtFonecom.SelectionStart = 3;
                }

                if (txtFonecom.TextLength >= 7 && txtFonecom.TextLength < 12)
                {
                    if (txtFonecom.Text.Substring(2, 1) == " ") //check if " " exists
                    { }
                    else //if doesn't exist, then add " " at index 2 (character no. 3)
                    {
                        txtFonecom.Text = txtFonecom.Text.Replace(" ", String.Empty);
                        txtFonecom.Text = txtFonecom.Text.Insert(2, " ");
                    }

                    txtFonecom.Text = txtFonecom.Text.Replace("-", String.Empty);   //now add "-" at index 7 (character no. 8)
                    txtFonecom.Text = txtFonecom.Text.Insert(7, "-");
                    txtFonecom.SelectionStart = txtFonecom.Text.Length;
                }

                if (txtFonecom.TextLength >= 12)
                {
                    if (txtFonecom.Text.Substring(2, 1) == " ") //check if " " exists
                    { }
                    else    //if doesn't exist, then add " " at index 2 (character no. 3)
                    {
                        txtFonecom.Text = txtFonecom.Text.Replace(" ", String.Empty);
                        txtFonecom.Text = txtFonecom.Text.Insert(2, " ");
                    }

                    txtFonecom.Text = txtFonecom.Text.Replace("-", String.Empty);   //now add "-" at index 8 (character no. 9)
                    txtFonecom.Text = txtFonecom.Text.Insert(8, "-");

                    txtFonecom.SelectionStart = txtFonecom.Text.Length;
                }
            }
        }

或者,如果您只想要textbox中的数字,请使用

if ((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57)