我在为TextBox构建输入过滤器时遇到了一些困难(WFP,但这不应该在这里)
我需要压制所有输入,但是:
-Digits(整数 - 没有浮点数,组)
- 基本数学符号:(,),+, - ,*,/
是的,目标是进行一种公式评估。这部分适用于Sriptcontrol-Method
(来自:http://www.vb-tips.com/Eval.aspx)
我还发现了几个导致过滤的正则表达式(对不起,记不住来源......):
Regex("[\d\(\)\+\-\*\/]")
Private Sub tb_PreviewKeyDown(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles TB0.PreviewKeyDown
Dim s As TextBox = sender
Debug.WriteLine(e.Key.ToString & " -" & e.Key)
Dim rx As New Regex("[\d\(\)\+\-\*\/]|Add|Subtract|Divide|Multiply|Return|Tab|Back")
If Not rx.IsMatch(e.Key.ToString) Then
e.Handled = True
Return
End If
If e.Key = Key.Enter Or e.Key = Key.Tab Then
If s.Text.Trim = "" Then Return
If Not IsNumeric(s.Text) Then
Try
Dim ev As New MSScriptControl.ScriptControl
ev.Language = "VBScript"
ev.AllowUI = False
ev.Reset()
s.Text = CStr(Math.Round(CDbl(ev.Eval(s.Text))))
s.SelectAll()
Catch ex As Exception
s.SelectAll()
e.Handled = True
Return
End Try
End If
s.SelectAll()
End If
End Sub
答案 0 :(得分:0)
Hans Passant的评论解决了我的问题!