Add VBScript scripting to your .NET app with the scripting control

Ever want to add macro or scripting support to your Visual Basic .NET application?

Ever want to add macro or scripting support to your Visual Basic .NET application? It's easy!

First (if you don't have it,) download the Script Control (ActiveX) from Microsoft for free at http://www.microsoft.com/downloads/details.aspx?FamilyId=D7E31492-2595-49E6-8C02-1426FEC693AC&displaylang=en

Now for an example.

Create a new VB Windows Forms Project. Open Form1 and add a textbox. Name the textbox "txtCode." Set its Multiline property to true and its scrollbars property to vertical. Also set tabstop=false.

Right click the controls pane and add the Script Control (It is a COM object called the 'Script Control Object') by choosing Add/Remove Items. Draw a script control on your form and name it scriptCon. (Note: You can use the language property to set the language to JScript if you like).

Now add a button btnExecute. Set the text property to 'Execute' and the TabStop property to False (This makes it easier to enter code).

Then enter the code following this article.

Upon execution, try pasting the following script in the code window and then clicking "Execute":

Function Main(i)
        MyClass.BackColor = RGB(0,0,0)
        MyClass.Text = "Hello World!"
End Function

You should be able to access all the properties and methods for whatever objects are passed into the Scripting control.

Private Sub btnExectute_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnExectute.Click

        'x(0) just creates a passable object array. This is optional in VB6, but not .NET
        Dim x(0) As Object


        'Give the object a value
        x(0) = 1
       
        'Add any objects you would like to be scripted
        'To the script control for scripting
        'For example, here I am passing the
        'Current form and telling the Script COntrol
        'That I want it to be referenced as "MYClass"
        'in the VBSCript code.
        scriptCon.AddObject("MYClass", Me)
        scriptCon.AddCode(txtCode.Text)
        'The first parameter in the run method is the name
        'of the first function to exectute
        'The second paramater allows you to
        'Pass in an array of parameters.
        scriptCon.Run("Main", x)
        scriptCon.Reset()
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Create a template for scripting
        txtCode.Text = "Function Main(i)" & vbCrLf & vbCrLf & "End Function"
    End Sub
This was first published in January 2005

Dig deeper on ASP.NET and Ajax development

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close