读书人

文本框显示数字格式?该如何处理

发布时间: 2013-11-08 17:52:35 作者: rapoo

文本框显示数字格式?
怎样设置?
1,234,567,890.55
[解决办法]
2500000.ToString("N")

2,500,000.00
[解决办法]
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public class Form1 : Form
{
TextBox tb = new TextBox();
public Form1()
{
tb.TextChanged += new EventHandler(tb_TextChanged);
Controls.Add(tb);
}

void tb_TextChanged(object sender, EventArgs e)
{
string value = tb.Text.Replace(",", "");
ulong ul;
if (ulong.TryParse(value, out ul))
{
tb.TextChanged -= tb_TextChanged;
tb.Text = string.Format("{0:#,#}", ul);
tb.SelectionStart = tb.Text.Length;
tb.TextChanged += tb_TextChanged;
}
}
}
}


http://social.msdn.microsoft.com/Forums/en-US/98887be8-814c-47e1-8110-c2442ce1640b/how-to-seprate-digits-with-comma-in-textbox
[解决办法]
private void textBox1_Leave(object sender, EventArgs e)
{
int num = int.Parse(textBox1.Text);
textBox1.Text = string.Format(num.ToString("N"));
}

读书人网 >C#

热点推荐