如 : text1=3*5*9*4 ,  text2=6*5*9 ,  text1,text2 数据不固定的.可能是3个、5个、、数据相乘 。
text3=text1*text2 ,  
text3=3*5*9*4*6*5*9=145800 
这样能做到吗?

解决方案 »

  1.   

    方法1:引用“Microsoft Script Control 1.0”
    Private Sub Command1_Click()
        Dim VBEval As New ScriptControl
        Dim Result As String
        On Error GoTo E
        VBEval.Language = "vbscript"
        Result = VBEval.Eval(Text1.Text)
        MsgBox Text1.Text & "=" & Result
        Exit Sub
    E:
        MsgBox "表达式错误,检查是否输入了非法字符!"
    End Sub
    方法2:
    Option Explicit
    Private Declare Function EbExecuteLine Lib "vba6.dll" (ByVal pStringToExec As Long, ByVal Unknownn1 As Long, ByVal Unknownn2 As Long, ByVal fCheckOnly As Long) As LongPrivate Function ExecuteLine(sCode As String, Optional fCheckOnly As Boolean) As Boolean
        ExecuteLine = EbExecuteLine(StrPtr(sCode), 0&, 0&, Abs(fCheckOnly)) = 0
    End Function
    Sub calc(ByVal x As String)
    Dim result
    ExecuteLine "dim x as double"
    ExecuteLine "x= " & x
    ExecuteLine "clipboard.settext x"
    result = Clipboard.GetText
    MsgBox x & "=" & result
    Set result = Nothing
    End SubPrivate Sub Command1_Click()
    calc Text1
    End SubPrivate Sub Form_Load()
    Text1.Text = "(1+(2*((3-2)-4)))/2" '只要是规则的表达式就可以
    End Sub
      

  2.   

    Private Sub Form_Load()
        Text1.Text = "3 * 5 * 9 * 4"
        Text2.Text = "6 * 5 * 9"
    End Sub'结果显示:145800
    Private Sub Command1_Click()
        MsgBox CalcText(Text1.Text) * CalcText(Text2.Text)
    End Sub'计算文本框类数字连乘函数
    Function CalcText(ByVal szText As String) As Long
        Dim ret
        Dim lngRet As Long
        Dim i As Long
        CalcText = 0
        If szText = "" Or InStr(szText, "*") = 0 Then
           Exit Function
        End If
        If InStr(szText, "*") <> 0 Then
           ret = Split(szText, "*")
           lngRet = 1
           For i = 0 To UBound(ret)
           Debug.Print ret(i)
               If IsNumeric(ret(i)) Then
                  lngRet = lngRet * ret(i)
               End If
           Next
           CalcText = lngRet
        End If
    End Function
      

  3.   

    忘记去掉Debug了:Private Sub Form_Load()
        Text1.Text = "3 * 5 * 9 * 4"
        Text2.Text = "6 * 5 * 9"
    End Sub'结果显示:145800
    Private Sub Command1_Click()
        MsgBox CalcText(Text1.Text) * CalcText(Text2.Text)
    End Sub'计算文本框类数字连乘函数
    Function CalcText(ByVal szText As String) As Long
        Dim ret
        Dim lngRet As Long
        Dim i As Long
        CalcText = 0
        If szText = "" Or InStr(szText, "*") = 0 Then
           Exit Function
        End If
        If InStr(szText, "*") <> 0 Then
           ret = Split(szText, "*")
           lngRet = 1
           For i = 0 To UBound(ret)
               If IsNumeric(ret(i)) Then
                  lngRet = lngRet * ret(i)
               End If
           Next
           CalcText = lngRet
        End If
    End Function
      

  4.   

    http://sgreport.nease.net/sourcecode/sourcecode0.html
      

  5.   


     tanaya(蜡笔小新)你好!
    text2改变text3即时更新 好象不行啊,请指教。如Private Sub Text2_Change()
    Text3.Text = CalcText(Text1.Text) * CalcText(Text1.Text)
    End SubFunction CalcText(ByVal szText As String) As Long
        Dim ret
        Dim lngRet As Long
        Dim i As Long
        CalcText = 0
        If szText = "" Or InStr(szText, "*") = 0 Then
           Exit Function
        End If
        If InStr(szText, "*") <> 0 Then
           ret = Split(szText, "*")
           lngRet = 1
           For i = 0 To UBound(ret)
               If IsNumeric(ret(i)) Then
                  lngRet = lngRet * ret(i)
               End If
           Next
           CalcText = lngRet
        End If
    End Function
      

  6.   

    DooDu的方法是杀鸡用了牛刀了:)楼主都说了是连乘了
      

  7.   

    To: 回复人: huajeisy() (   可以即时更新啊,你那代码怎么都改呢,呵呵

    Private Sub Text2_Change()
        Text3.Text = CalcText(Text1.Text) * CalcText(Text1.Text)
    End Sub改为:Private Sub Text2_Change()
        Text3.Text = CalcText(Text1.Text) * CalcText(Text2.Text)
    End Sub
      

  8.   

    啊,写错了,已经OK了,谢谢各位,还有一个问题请教各位.
    如:text1=text2/text3 =1/2 后 text1=50%  这样的效果要怎样搞, 我只是改了TEXT1的属性为%这样类型.但还不会显示%.  这个OK就结贴. 谢谢。
      

  9.   

    除了这种方法还有其它的吗? Text1 = (Text2 / Text3) * 100 & "%"