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? 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)
        '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)
    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

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.