【发布时间】:2015-08-16 19:42:41
【问题描述】:
我想在文本框中输入数字,我的文本框会自动将其转换为逗号(,)格式。我试图这样做,但它工作错了。帮我?像这样 1,20(我只输入 120);
private bool IsNumeric(int Val)
{
return ((Val >= 48 && Val <= 57) || (Val == 8) || (Val == 46));
}
String str;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
int KeyCode = e.KeyValue;
if (!IsNumeric(KeyCode))
{
if (KeyCode == 13)
{
e.Handled = true;
vendas();
str = null;
}
e.Handled = true;
return;
}
else
{
e.Handled = true;
}
if (((KeyCode == 8) || (KeyCode == 46)) && (str.Length > 0))
{
str = str.Substring(0, str.Length - 1);
}
else if (!((KeyCode == 8) || (KeyCode == 46)))
{
str = str + Convert.ToChar(KeyCode);
}
if (str.Length == 0)
{
textBox1.Text = "";
}
if (str.Length == 1)
{
textBox1.Text = "0,0" + str;
}
else if (str.Length == 2)
{
textBox1.Text = "0," + str;
}
else if ((str.Length > 2) && (str.Length != 6) && (str.Length != 9) && (str.Length != 12))
{
textBox1.Text = str.Substring(0, str.Length - 2) + "," + str.Substring(str.Length - 2);
textBox1.Text = textBox1.Text;
}
else if ((str.Length > 6) && (str.Length != 8) && (str.Length != 10) && (str.Length != 12))
{
textBox1.Text = str.Substring(0, str.Length - 3) + "," + str.Substring(str.Length - 1);
textBox1.Text = textBox1.Text;
}
}
它显示的是 10,01 而不是 0,01?
【问题讨论】:
-
疯狂的缩进是怎么回事?
-
您在某处错误地指定了 Substring 的参数。另外,为什么不直接使用内置的数字格式化功能呢?
-
我正在做现金柜台程序,这就是我需要它的原因。对于 exp:总价为 10,50,我需要写入文本框 1050,但文本框会自动将其转换为相同的数字;
标签: c#