What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleGraph a user-entered function by using the MS Script control
DescriptionThis example shows how to graph a user-entered function by using the MS Script control in Visual Basic 6. The program steps through different X values, using the Script control to evaluate the function for each.
Keywordsgraph, equation, scale, user-entered function, mathematical expression, expression, evaluate, parse, Script control
CategoriesStrings, Algorithms, Graphics
When the user clicks the Graph button, the program reads the X and Y bounds. It uses the picGraph control's Scale method to map these bounds onto the control's surface.

The program then clears the control and draws axes and tick marks.

Next the program creates an MS Script control and sets its language to VB Script. It then creates a function TheFunction that takes a parameter X and returns the result given by the user-entered expression.

The program then loops over the desired X values, using the Script control's Eval method to evaluate TheFunction for the new value of X, and graphing the result.

' Graph the equation entered by the user.
Private Sub cmdGraph_Click()
Dim xmin As Single
Dim xmax As Single
Dim Dx As Single
Dim ymin As Single
Dim ymax As Single
Dim x As Single
Dim y1 As Single
Dim y2 As Single
Dim i As Integer
Dim script_control As MSScriptControl.ScriptControl

    On Error GoTo EvaluateError

    ' Get the bounds.
    xmin = CSng(txtXmin.Text)
    xmax = CSng(txtXmax.Text)
    Dx = CSng(txtDx.Text)
    ymin = CSng(txtYmin.Text)
    ymax = CSng(txtYmax.Text)

    ' Scale to fit the bounds.
    picGraph.Scale (xmin, ymax)-(xmax, ymin)

    ' Draw the graph's background.
    picGraph.Line (xmin, 0)-(xmax, 0), vbBlue
    picGraph.Line (0, ymin)-(0, ymax), vbBlue
    For i = 0 To xmin Step -1
        picGraph.Line (i, -0.25)-Step(0, 0.5), vbRed
    Next i
    For i = 0 To xmax
        picGraph.Line (i, -0.25)-Step(0, 0.5), vbRed
    Next i
    For i = 0 To ymin Step -1
        picGraph.Line (-0.25, i)-Step(0.5, 0), vbRed
    Next i
    For i = 0 To ymax
        picGraph.Line (-0.25, i)-Step(0.5, 0), vbRed
    Next i

    ' Make a script control.
    Set script_control = New MSScriptControl.ScriptControl
    script_control.Language = "VBScript"

    ' Define the function.
    script_control.AddCode _
        "Function TheFunction(X)" & vbCrLf & _
        "    TheFunction = " & txtExpression.Text & vbCrLf _
            & _
        "End Function"

    ' Graph the equation.
    y1 = script_control.Eval("TheFunction(" & xmin & ")")
    For x = xmin + Dx To xmax Step Dx
        ' Evaluate the next value.
        y2 = script_control.Eval("TheFunction(" & x & ")")

        ' Draw the next line.
        picGraph.Line (x - Dx, y1)-(x, y2)

        ' Save the current Y value.
        y1 = y2
    Next x
    Exit Sub

    MsgBox Err.Description
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.