1个回答
展开全部
1)在窗体Form1上布置一个TextBox控件
2)对textBox1的KeyPress事件和TextChanged编程。完整代码如下
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "";
}
private void textBox1_KeyPress(object sender,
KeyPressEventArgs e)
{
// 只允许输入数字和Del
if (!char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
{
e.Handled = true;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text)) return;
// 按千分位逗号格式显示!
double d = Convert.ToDouble(skipComma(textBox1.Text));
textBox1.Text = string.Format("{0:#,#}", d);
// 确保输入光标在最右侧
textBox1.Select(textBox1.Text.Length, 0);
}
private string skipComma(string str)
{
string[] ss = null;
string strnew = "";
if (str == "")
{
strnew = "0";
}
else
{
ss = str.Split(',');
for (int i = 0; i < ss.Length; i++)
{
strnew += ss[i];
}
}
return strnew;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询