1

当总长度 == 11 时,我编写了格式化数字的代码,它在 texbox 更改时运行,但只有在它有 11 个字符时才格式化,我想在运行时(实时)进行,明白吗?这是可能的 ?查看我的代码:

private void textBox3_TextChanged(object sender, EventArgs e)
        {

            Int64 cpf = Convert.ToInt64(textBox3.Text);
            if (textBox3.TextLength == 11)
            {
                textBox3.Text = string.Format(@"{0:000\.000\.000-00}", Convert.ToInt64(cpf));
            }
        }

谢谢

4

2 回答 2

1

正如lazyberezovsky 所说,使用带掩码的文本框,但将PromptChar设置为您想要的任何内容。类似于以下内容:

//In your form_load
//Based on your code above, assuming textBox3 is a MaskedTextbox    
textBox3.KeyUp += CheckEvent()
textBox3.Mask = "000000000000";
textBox3.PromptChar = 'x'; //set this to a space or whatever you want ' ' for blank!

//check AFTER every key press
private void CheckEvent(object Sender, KeyEventArgs e)
{
    if(textBox3.Text.Count() < 12)
    {
        return;
    }

    //change the textboxMask when all chars present
    maskedTextBox1.Mask = "0:000.000.000-00";
}
于 2013-04-13T12:03:55.170 回答
0

考虑使用MaskedTextboxMask等于000.000.000-00。它将以通常的方式从左到右填充掩码。输入将如下所示:

___.___.___-__

当使用类型1时,它会显示1__.___.___-__. 当使用类型12时,它会显示12_.___.___-__. 等等。

于 2013-04-13T11:51:33.747 回答