Home > Microsoft .Net Development Tips > Visual Basic and Visual Basic .NET > Add VBScript scripting to your .NET app with the scripting control
Win Development Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

VISUAL BASIC AND VISUAL BASIC .NET

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


John Plante
01.18.2005
Rating: -4.67- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.

Code

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

Rate this Tip
To rate tips, you must be a member of SearchWinDevelopment.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Visual Basic and Visual Basic .NET
Compose XML more quickly using Visual Basic 9
Virtualization keeps Legacy Apps alive
VB 9 Anonymous Types help create flexible objects
Visual Studio Team System Add-ins: Conchango Scrum for Team System and Scrum Dashboard
Book Excerpt: Sams Teach Yourself Visual Basic 2008 in 24 Hours -- Complete Starter Kit
Check out CodePlex for a ton of interesting .NET projects
Book excerpt: Murach's VB 2008
Book excerpt: Printing in Visual Basic 2005
Visual Basic 2008 and closures
WinForms development using SQL Server 2005 and Visual Basic 2005

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts