Here is some sample code I created a few days back.
This is a WinForm application.

Watch for line breaks where they don't belong. (Wonderfull email)

Make sure you include a reference to the MSScriptingControl:
1) Right click on 'References' in the Solution Explorer
2) Click on Add Reference
3) Click on the COM tab
4) Double click on the 'Microsoft Script Control 1.0'
   This will import it into your project.
5) Click OK

Then use the code below to test it.

I love how you can add .NET objects into the script engine by just adding
public functions.

WAY COOL!

Here is my code for reference:
------------------------------------------
(Form1.cs)
------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ScriptTest1
{
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
                private System.Windows.Forms.Label label1;
                private System.Windows.Forms.Button runButton;
                private System.Windows.Forms.Button closeButton;
                private System.Windows.Forms.TextBox sourceCode;
                private MSScriptControl.ScriptControl iscr;
                /// <summary>
                /// Required designer variable.
                /// </summary>
                private System.ComponentModel.Container components = null;

                public Form1()
                {
                        //
                        // Required for Windows Form Designer support
                        //
                        InitializeComponent();

                        //
                        // TODO: Add any constructor code after
InitializeComponent call
                        //
                }

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                protected override void Dispose( bool disposing )
                {
                        if( disposing )
                        {
                                if (components != null)
                                {
                                        components.Dispose();
                                }
                        }
                        base.Dispose( disposing );
                }

                #region Windows Form Designer generated code
                /// <summary>
                /// Required method for Designer support - do not modify
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                        this.label1 = new System.Windows.Forms.Label();
                        this.runButton = new System.Windows.Forms.Button();
                        this.closeButton = new
System.Windows.Forms.Button();
                        this.sourceCode = new
System.Windows.Forms.TextBox();
                        this.SuspendLayout();
                        //
                        // label1
                        //
                        this.label1.BackColor =
System.Drawing.SystemColors.Window;
                        this.label1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
                        this.label1.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
                        this.label1.Location = new System.Drawing.Point(8,
184);
                        this.label1.Name = "label1";
                        this.label1.RightToLeft =
System.Windows.Forms.RightToLeft.No;
                        this.label1.Size = new System.Drawing.Size(448,
216);
                        this.label1.TabIndex = 0;
                        //
                        // runButton
                        //
                        this.runButton.Location = new
System.Drawing.Point(8, 152);
                        this.runButton.Name = "runButton";
                        this.runButton.TabIndex = 1;
                        this.runButton.Text = "Run";
                        this.runButton.Click += new
System.EventHandler(this.runButton_Click);
                        //
                        // closeButton
                        //
                        this.closeButton.Location = new
System.Drawing.Point(384, 152);
                        this.closeButton.Name = "closeButton";
                        this.closeButton.TabIndex = 2;
                        this.closeButton.Text = "Close";
                        this.closeButton.Click += new
System.EventHandler(this.closeButton_Click);
                        //
                        // sourceCode
                        //
                        this.sourceCode.AcceptsTab = true;
                        this.sourceCode.AllowDrop = true;
                        this.sourceCode.Location = new
System.Drawing.Point(8, 8);
                        this.sourceCode.Multiline = true;
                        this.sourceCode.Name = "sourceCode";
                        this.sourceCode.ScrollBars =
System.Windows.Forms.ScrollBars.Both;
                        this.sourceCode.Size = new System.Drawing.Size(448,
136);
                        this.sourceCode.TabIndex = 3;
                        this.sourceCode.Text = "var out = \"\";\r\nout =
\"hello from jscript\\r\\n\";\r\n\r\nfor( i = 0; i < 10; i++ )\r\n{\r\n\t" +
                                "out += \"This is the output for line:
\"+(i+1)+\" - [\"+obj.Add(i,i)+\"]\"+\"\\r\\n\";\r\n}\r" +
                                "\n\r\ntest.Write( out );\r\ntest.Alert(
\"This is a popup!\" );";
                        this.sourceCode.TextChanged += new
System.EventHandler(this.sourceCode_TextChanged);
                        //
                        // Form1
                        //
                        this.AutoScaleBaseSize = new System.Drawing.Size(5,
13);
                        this.ClientSize = new System.Drawing.Size(464, 406);
                        this.Controls.AddRange(new
System.Windows.Forms.Control[] {

this.sourceCode,

this.closeButton,

this.runButton,

this.label1});
                        this.Name = "Form1";
                        this.Text = "Form1";
                        this.Load += new
System.EventHandler(this.Form1_Load);
                        this.ResumeLayout(false);

                }
                #endregion

                /// <summary>
                /// The main entry point for the application.
                /// </summary>
                [STAThread]
                static void Main()
                {
                        Application.Run(new Form1());
                }

                private void Form1_Load(object sender, System.EventArgs e)
                {
                        iscr = new MSScriptControl.ScriptControlClass();
                        iscr.Language = "jscript";
                        iscr.AddObject( "obj", new TestThing( this ), true
);
                        iscr.AddObject( "test", this, true );
                }

                public void Clear()
                {
                        this.label1.Text = "";
                }

                public void Write( string s )
                {
                        this.label1.Text += s;
                }

                public void Alert( string s )
                {
                        MessageBox.Show( s );
                }

                private void runButton_Click(object sender, System.EventArgs
e)
                {
                        iscr.ExecuteStatement( this.sourceCode.Text );
                }

                private void closeButton_Click(object sender,
System.EventArgs e)
                {
                        this.Close();
                }

                private void sourceCode_TextChanged(object sender,
System.EventArgs e)
                {

                }
        }

        public class TestThing
        {
                Form1 form;
                public TestThing( Form1 f )
                {
                        form = f;
                }

                public int Add( int a, int b )
                {
                        return a+b;
                }
        }
}
------------------------------------------

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to