我尝试添加按钮以在其中添加逗号,如下所示:
protected void Button1_Click(object sender, System.EventArgs e) {
Number.Text = "";
string s = string.Format("{0:n0}",1234567890);
Number.Text += "value : 1234567890";
Number.Text += "<br />Thousands Separator: " + s;
Number.Text += "<br/><br />another solution";
int value = 1234567890;
string formatedstring = value .ToString("#,##0");
Number.Text += "<br />result: " + formatedstring;
}
但是,我希望输入后立即更改它,类似于Unity中的更新功能。添加按钮功能非常不道德。
最佳答案
为文本框中的文本更改创建事件。然后添加以下代码:
void numberTextBox_TextChanged(object sender, EventArgs e)
{
string value = numberTextBox.Text.Replace(",", "");
ulong ul;
if (ulong.TryParse(value, out ul))
{
numberTextBox.TextChanged -= numberTextBox_TextChanged;
numberTextBox.Text = string.Format("{0:#,#}", ul);
numberTextBox.SelectionStart = numberTextBox.Text.Length;
numberTextBox.TextChanged += numberTextBox_TextChanged;
}
}