帮我解释一段VB代码

来源:百度知道 编辑:UC知道 时间:2024/06/24 05:22:34
加入详细的注释让我明白就OK啦
Private Sub Command1_Click()
Text4.Text = strCalc(Text1.Text & Text2.Text & Text3.Text)
End Sub

Public Function strCalc(Tmpstr$) As Double
Dim sc
Set sc = CreateObject("ScriptControl")
sc.Language = "VBScript"
strCalc = sc.Eval(Tmpstr)
Set sc = Nothing
End Function

Private Sub Command1_Click()
'调用一个函数,把三个文本框的内容合并,作为参数传给这个函数,返回的结果写入Text4
Text4.Text = strCalc(Text1.Text & Text2.Text & Text3.Text)
End Sub

'这个函数的作用是计算表达式的值
Public Function strCalc(Tmpstr$) As Double
Dim sc
'建立一个ScriptControl对象,这个对象最主要的作用就是计算表达式的值
Set sc = CreateObject("ScriptControl")
'设置语言
sc.Language = "VBScript"
'运算,比如你要计算的字符串是"2+3",那么下面的运算就会返回5
strCalc = sc.Eval(Tmpstr)
'释放对象
Set sc = Nothing
'函数结束,返回运算结果
End Function

楼上的正解

ok,我来试一试。
Private Sub Command1_Click() '单击command1时触发
Text4.Text = strCalc(Text1.Text & Text2.Text & Text3.Text) '执行自己的函数strCalc,把Text1.Text & Text2.Text & Text3.Text的内容作为参数传过去,进行下述处理
End Sub '过程结束

Public Function strCalc(Tmpstr$) As Double '开始一个函数
Dim sc '定义一个变量sc
Set sc = CreateObject("ScriptControl") 'CreateObject的作用是创建部件对象