【发布时间】:2012-02-02 05:38:41
【问题描述】:
我正在尝试在 WPF 中仅创建一个数字 TextBox,并且我有以下代码:
void NumericTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsValidInput(e.Text);
}
private bool IsValidInput(string p)
{
switch (this.Type)
{
case NumericTextBoxType.Float:
return Regex.Match(p, "^[0-9]*[.][0-9]*$").Success;
case NumericTextBoxType.Integer:
default:
return Regex.Match(p, "^[0-9]*$").Success;
}
}
// And also this!
public enum NumericTextBoxType
{
Integer = 0,
Float = 1
}
当我将类型设置为 Integer 时,它工作得很好,但对于 Float,它就不行了。
我可以使用这么多NumericTextBox 控件,但我想知道为什么这个不起作用?
【问题讨论】:
-
具体在什么情况下不起作用?
-
对于
Integer,它接受整数,但对于Float,它只接受一个句点.。 -
看起来 Double.TryParse 会更好地工作...但与 RegExp 相比,乐趣要少得多...
标签: c# wpf regex validation