我有一个应该只接受数字的TextBox (它们可以是简单的int或float,功率因数为E)。
因此,一些有效的例子是:
1
+1.1
0
.1
1E-1
1e-2
-1.66
-6e-10无效示例:
E-1通常,它应该以数字或+和-号或点开头。
然后,我将把它放在TextChanged事件中,这样就没问题了。
textBox.Text = Regex.Replace(textBox.Text, @"[\d]", ""); //so useless!发布于 2012-01-04 20:41:15
如果你想使用你的指数,试试这个(一定要添加using System.Globalization;):
decimal value;
if(Decimal.TryParse(textBox.Text, NumberStyles.Float, CultureInfo.CurrentCulture, out value))
{
textBox.Text = value.ToString();
}发布于 2012-01-04 20:34:40
为什么要使用Regex呢?
float f;
bool isNumber = Single.TryParse(textbox.Text, out f);https://stackoverflow.com/questions/8726855
复制相似问题