Library tutorials & articles

Using .NET to make your Application Scriptable

Finishing Off

The only part left to do is to finish off the main form by making the Edit Script button work as it should. This means showing the dialog and, if the compile was successful, storing the compiled script and initialising it.

[VB]
Private Sub btnEditScript_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnEditScript.Click
    Dim f As New frmScript()
    'Show script editing dialog with current script source
    f.ScriptSource = ScriptSource
    If f.ShowDialog(Me) = DialogResult.Cancel Then Return
    'Update local script source
    ScriptSource = f.ScriptSource
    'Use the compiled plugin that was produced
    Script = f.CompiledScript
    Script.Initialize(Me)
End Sub

[C#]
private void btnEditScript_Click(object sender, System.EventArgs e)
{
    frmScript f = new frmScript();
    // Show script editing dialog with current script source
    f.ScriptSource = ScriptSource;
    if (f.ShowDialog(this) == DialogResult.Cancel)
        return;
    // Update local script source
    ScriptSource = f.ScriptSource;
    // Use the compiled plugin that was produced
    Script = f.CompiledScript;
    Script.Initialize(this);
}

That's it! When you open the sample solutions you'll notice that I wrote a blank, template script that simply provides a blank implementation of IScript and embedded it as a text file in the host application. Code in the constructor of the main form sets the initial script source to this. You'll also notice that I used VB scripting for this example, but changing one line of code and writing a new default script is all you need to do to make it use C# code to script instead.

To try it out, run the solution. Click the Edit Script button on the main form and put whatever you like in the four methods. You can use Host.ShowMessage to cause the host application to show a messagebox and Host.TextBox to use that textbox on the main form. Press Ok, and assuming the compile succeeds you can press the four buttons to run the four corresponding procedures in the script you just compiled.

In the real world the scripting interfaces would obviously be much more complicated than this, but this should give you a pretty good idea of how to compile on the fly and hook up a compiled script to your application. I have provided both a VB and C# solution to go with this article, which are functionally identical. Open the Scripting.sln file in the Scripting directory.

Comments

  1. 17 Dec 2005 at 10:07

    How can we expose host application objects to the script that we dynamically compile?

  2. 12 Dec 2005 at 06:17

    How can we expose object in the host application to the script that we dynamically compile?

  3. 01 Jan 1999 at 00:00

    This thread is for discussions of Using .NET to make your Application Scriptable.

Leave a comment

Sign in or Join us (it's free).

Tim Dawson
AddThis

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

We'd love to hear what you think! Submit ideas or give us feedback