文本框显示格式 [英] Textbox display formatting
问题描述
我希望每个组3位数后添加,来。例如:当我输入3000000文本框会显示300万,但仍有价值为300万。结果
我试图用maskedtexbox,有一个缺点,即maskedtexbox显示的号码例如的 _ __ __
I want to add "," to after every group of 3 digits. Eg : when I type 3000000 the textbox will display 3,000,000 but the value still is 3000000.
I tried to use maskedtexbox, there is a drawback that the maskedtexbox displayed a number like _,__,__ .
推荐答案
尝试添加该代码的KeyUp
你的文本框
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text))
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
int valueBefore = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
textBox1.Text = String.Format(culture, "{0:N0}", valueBefore);
textBox1.Select(textBox1.Text.Length, 0);
}
}
是的,它会改变存储在texbox值,但每当你需要的实际数量,你可以使用下面的行从文本得到它:
Yes, it will change the value stored in a texbox, but whenever you need the actual number you can use the following line to get it from the text:
int integerValue = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
当然不要忘了检查一下用户输入到文本框中实际上是一个有效的整数
Of course do not forget to check that what the user inputs into the textbox is actually a valid integer number.
这篇关于文本框显示格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!