答案 0 :(得分:0)
每次击键都会触发Keypress事件,因此您可以将格式代码放在该事件处理程序中,以确保格式化代码在文本框的任何更改中运行
答案 1 :(得分:0)
键盘类型事件期间:
int theinteger
textbox.Text
string.Format("{0:n0}", theinteger);
答案 2 :(得分:-1)
public void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if ((int)(e.KeyChar) != 8)
{
if ((((int)(e.KeyChar) < 48 | (int)(e.KeyChar) > 57) & (int)(e.KeyChar) != 46) | ((int)(e.KeyChar) == 46 & textBox1.Text.Contains(".")))
{
e.Handled = true;
}
else
{
if (!textBox1.Text.Contains("."))
{
int iLength = textBox1.Text.Replace(",", "").Length;
if (iLength % 3 == 0 & iLength > 0)
{
if ((int)(e.KeyChar) != 46)
{
textBox1.AppendText(",");
}
}
}
}
}
else
{
if (textBox1.Text.LastIndexOf(",") == textBox1.Text.Length - 2 & textBox1.Text.LastIndexOf(",") != -1)
{
textBox1.Text = textBox1.Text.Remove(textBox1.Text.LastIndexOf(","), 1);
textBox1.SelectionStart = textBox1.Text.Length;
}
}
}