我正在尝试在 Visual Basic 中制作一个(非常简单的)图形计算器,并且我使用了 ScriptControl 以便用户可以输入他们自己的方程式并使用它eval()
来获得结果。
Public Class Form1
Dim sc As MSScriptControl.ScriptControl = New MSScriptControl.ScriptControl
Private Sub Form1_KeyPressed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.KeyPress, Me.Load
sc.Language = "VBScript"
sc.AddObject("x", 355, True)
sc.AddCode("Sub setx(xvalo)" + vbCrLf + "x = xvalo" + vbCrLf + "End Sub")
Me.CreateGraphics.DrawLine(Pens.Black, New Point(0, 200), New Point(5000, 200))
Dim eq As String = InputBox("Please enter equation:")
Dim y As Double = 0
For i As Double = 0 To 50 Step 0.01
sc.Run("setx", i)
y = sc.Eval(eq)
Me.CreateGraphics.DrawLine(Pens.Blue, New Point(i * 10, y * 10 + 200), New Point(i * 10 + 0.1, y * 10 + 0.1 + 200))
Next
End Sub
End Class
当我运行它并输入x + 2
或什至只是x
在提示框中输入时,我得到一个异常:NotSupportedException: Object doesn't support this property or method: 'x'
但是我之前添加了 x ......有人知道如何解决这个问题吗?
还有一件奇怪的事情:似乎每当我尝试编辑sc.AddCode()
参数时,它都会显示COMException was unhandled
.