Textbox display formatting

前端 未结 5 1356
花落未央
花落未央 2020-12-19 21:30

I want to add \",\" to after every group of 3 digits. Eg : when I type 3000000 the textbox will display 3,000,000 but the value still is 3000000.
I tried to use maskedte

5条回答
  •  臣服心动
    2020-12-19 21:41

    You could hook up to OnKeyUp event like this:

     private void textBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if (!(e.KeyCode == Keys.Back))
                {
                    string text = textBox1.Text.Replace(",", "");
                    if (text.Length % 3 == 0)
                    {
                        textBox1.Text += ",";
                        textBox1.SelectionStart = textBox1.Text.Length;
                    }
                }
            }
    

提交回复
热议问题