0

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

2 回答 2

0

我建议不要手动处理按键,而是使用带有掩码的MaskedTextBox00 0000-0000控件,因为该控件可以自动为您设置输入格式。

假设您仍然更喜欢使用 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;
        }
    }
}

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

于 2013-04-14T04:57:32.710 回答
0

无论用户在任何地方删除或添加数字,此代码都会将" "and保留在其位置:"-"

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)
于 2013-04-14T05:19:30.110 回答