如何设置文本框仅接受带有两个小数位的数字 [英] How to set the text box accept only numbers with two decimal places
本文介绍了如何设置文本框仅接受带有两个小数位的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
我想知道是否有办法只在我的文本框中接受只有两位小数的数字(例如:123.55)。我还没有尝试任何代码,但我搜索的是使用蒙版文本框。但我不喜欢使用它,因为我不知道它:)任何人都可以帮助我吗?在此先感谢!
Hi guys!
I would like to know if there is any way to accept only numbers with two decimal places (example: 123.55) only on my text boxes. I have not yet tried any code but what I have searched is using the masked text box. But I do not like to use it due to the fact that I do not know about it :) anyone can help me? Thanks in advance!
推荐答案
写下代码文本框按键事件如下
Writre code textbox keypress event like Below
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
这篇关于如何设置文本框仅接受带有两个小数位的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文