之间输入号码,我怎样才能限制进入任何东西,在文本框中99.99如何只允许0-99.99
我的代码似乎是工作的用户,比0之间的值,但其他的,它会允许输入任意号码。它只允许一次数字和一个点。但它仍然会允许的值大于99.99
下面我的代码更大:
private void InputMargin_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
// allow only number and dot
if ( ! char.IsControl(e.KeyChar)
&& ! char.IsDigit(e.KeyChar)
&& e.KeyChar != '.'
|| (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
)
{
//e.Handled = true;
double margin;
double.TryParse((sender as TextBox).Text, out margin);
if (margin >= 0 && margin <= 99.99)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
}
catch
{
e.Handled = false;
}
}
为什么你不应该使用蒙面文本框? –
为什么不试试像这样的'[0-9] {1,2} [。] {0,1} [0-9] {2}''的正则表达式。这可能不是正确的选择,但可以作为出发点 – Saravanan