Click here to Skip to main content
15,879,184 members
Articles / Desktop Programming / Windows Forms
Article

.Net Script Editor (C#,Vb.net Mini IDE)

Rate me:
Please Sign up or sign in to vote.
3.93/5 (44 votes)
13 Jul 2008CPOL6 min read 241.8K   29.3K   152   41
The source code / article published here is to provide features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc

Add scripting functionality to your application,features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. Mini .Net IDE in single winform control.

Image 1

Introduction

The actual requirement of this control is to build scripting feature in my application as available in all Microsoft Office applications (VBA) and is alternative to Microsoft product VSA (Microsoft Visual Studio for Applications).

The source code / article published here is to provide features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. The control supports C# and VB.net language however can be easily modified to support any .net language. You can use COM references, .Net references & Web References. The control supports Visual studio IDE features like bookmark, highlighting interpreter errors & compiler errors, adding multiple classes and interfaces. You can imagine visual studio IDE build in single control with feature like AddObject from MS Script Control. To use this control you just need to drag and drop it to your winforms and add few lines of code to add instance of object which you want to access in the scripts.

VSA History

The actual requirement of this control is to build scripting feature in my application as available in all Microsoft Office applications (VBA) and is alternative to Microsoft product VSA (Microsoft Visual Studio for Applications)

Many developers are looking for scripting solution and are very disappointed with Microsoft for not providing any equivalent feature of Microsoft scripting control in .Net framework. Script controls have some very nice features like AddObject, which allows you to add object instance (at run time) to the scripting engine which can be accessed by the script. For more details on VBScript Editor with Intellisense using MS Script control check my previously published article on plant source code.

Some developer’s tries to use MS Scripting control using .net interop however not all the features are available and it is quite unstable. AddObject method can’t be used to access .net objects.

Microsoft has replaced VBA (Visual Basic for applications ) with VSA (Visual Studio for Applications) VSA namespace was available in versions before .net 2.0, unfortunately vsa namespace is now obsolete (in .net 2.0 and further versions).

Microsoft has released separate product “Microsoft Visual Studio for Applications” to add scripting capabilities to your applications like VBA, however you can now use C#, vb.net etc in VSA editor. VSA is shipped with SQL server 2005 Integration services (SSIS), which is another name of DTS (SQL Server 2000). While designing package in SSIS you can use VSA editor to write .Net code. You need to buy this (VSA) product if you want to use it in your product/application.

Using the code

Note: The complete source code for ScriptControl is published on sourceforge.net and only wrapper project ( and binaries) to test the control is posted on code project.com

The source code / article published here is to provide features of MS Script control (Functions like AddObject) and some features of VSA/Visual Studio editor like intellisence, code completion etc. The control supports C# and VB.net language however can be easily modified to support any .net language. You can use COM references, .Net references & Web References. The control supports Visual studio IDE features like bookmark, highlighting interpreter errors & compiler errors, adding multiple classes and interfaces. You can imagine visual studio IDE build in single control with feature like AddObject from MS Script Control. To use this control you just need to drag and drop it to your winforms and add few lines of code to add instance of object which you want to access in the scripts.

When you compile the scripts it will generate AIMSScript.dll assembly, which holds you compiled .net code. When you click on execute button the assembly will get loaded into separate appdomain and will be execute there and the appdomain is then unloaded completely as there no way to unload single assembly from appdomain.

The published source code (on sourceforge.net) contains following sub projects

1. Script Control:
Description: Main control which provide visual studio like IDE in single window control. It uses all below mentioned components to provide the functionality. Auto list window, code compilation feature, Intellesense feature, com, .net and web reference feature etc are all implemented in this control. This is based on libraries from SharpDevelop open source C# IDE
2. Script Engine
Description: This is control used to execute your assembly generated from script code in separate App Domain and returns the exit code to calling program. It uses IRun Interface, which is part of ScriptRun project (Mentioned below) for cross App Domain communication.
3. Script Run
Description: This project exposes IRun interface and ScriptInstance (drived from MarshalByRefObject) class and is used to communicate with cross app domains using remoting.
4. Code Editor
Description: Code Editor control is control where you write your code. It uses syntaxdocument to implement code colour in the editor window.
The syntax highlight support library is based on Fireball.CodeEditor . Some features/concepts like completion window, insight window of code editor control are derived from SharpDevelop open source project.
5. SyntaxDocument
Description: SyntaxDocument project is DOM implementation to support colour codes for various languages. This library is based on open source project Fireball.CodeEditor.
6. SyntaxFiles
Description: The project assembly persists various colour rules for each language. It supports varieties of Syntax Files for languages like C#, CPP, CSS, Delphi, Fortran, foxpro, HTML, Java, Lotus Script, PHP and lot more. Each syntax file holds rules for colour coding for particular language in XML format which can be applied to CodeEditor control using CodeEditorSyntaxLoader class. This library is based on open source project Fireball.CodeEditor .
7. Docking
Description:The docking library provides GUI controls like Auto Hide Strips, dock pane, dock windows etc. and is based on libraries from open source project dockpanelsuite and Fireball.CodeEditor

8. Test – Project to test control
Description: This is the startup project and contains single form to host the script control and Engine control.Following code is used to link script control and engine control.

C#
public <place />Main</place />()
        {
            InitializeComponent();
            scriptControl1.AddObject("Container", this);
            scriptControl1.StartEditor();
            scriptControl1.Execute += new EventHandler(scriptControl1_Execute);
        }
        void scriptControl1_Execute(object sender, EventArgs e)
        {
            try
            {
                engine1.OutputAssemblyName = scriptControl1.OutputAssemblyName;
                engine1.StartMethodName = scriptControl1.StartMethodName;
                engine1.DefaultNameSpace = scriptControl1.DefaultNameSpace;
                engine1.RemoteVariables = scriptControl1.RemoteVariables;
                engine1.DefaultClassName = scriptControl1.DefaultClassName;




                object ret = engine1.Execute(null);
                MessageBox.Show("Return Code : " + ret.ToString());

            }
            catch
            {
            }
        }        

Points of Interest : AddObject Feature

Note that scriptControl1.AddObject("Container", this); code is adding instance of parent form to be accessed by script by name "Container".If you type code like Container.Text = "Hi"; it will change the caption of parent form.

To implement AddObject feature following code is used which is hidden in file Program.sys:This class is drived from MarshalByRefObject and implements <city><place>Irun interface for cross appdomain communication.

C#
#region System Generated Source Code.Please do not change ...

namespace AIMS.Script
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Reflection;
    public partial class Program : MarshalByRefObject, <city /><place />IRun
    {
        public static System.Windows.Forms.Form Container = null;

         [DebuggerStepperBoundary()]

        void IRun.Initialize(IDictionary<string, object> Variables)
        {  foreach (string name in Variables.Keys)
            { object value = null;
                try { Variables.TryGetValue(name, out value);
                 FieldInfo fInfo = this.GetType().GetField(name, BindingFlags.Public | BindingFlags.Static);
                      fInfo.SetValue(this, value);
               }
               catch (Exception ex)
                {
                   throw ex;
                }
           }
        }
        [DebuggerStepperBoundary()]
        object IRun.Run(string StartMethod, params object[] Parameters)
        {try
           {MethodInfo methodInfo = this.GetType().GetMethod(StartMethod, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);
                  return methodInfo.Invoke(this, Parameters);
           }
          catch (Exception ex)
            {
              throw ex;

            }
      }
       [DebuggerStepperBoundary()]
       void IRun.Dispose(IDictionary<string, object> Variables)
       {
           foreach (string name in Variables.Keys)
            {
             object value = null; 
               try
               {FieldInfo fInfo = this.GetType().GetField(name, BindingFlags.Public | BindingFlags.Static);

                       fInfo.SetValue(this, value);
                }
              catch (Exception ex)
               {
                  throw ex;
               }
            }
       }
    }
}
#endregion

It is not possible to provide all technical implementation (of this project) in this article but I promises to publish further information if you are really interested in this control. Your feed backs are very valuable and keeps me motivating to send some time on such projects and publish them, so please don’t forget to add your feedback.

Further Enhancements:

1. Support for debugger & break points

- Current project allows you to mark break point in GUI only however you can use debugger.break function to break the code at any line and if .Net is installed on your machine its .net debugger will be attached to your assembly. SharpDevelop IDE support debugging and it can be implemented in the system.

2. Form Designer :

- Form designer was not required in my application and hence I have not implemented, However you may implement form designer component (see SharpDevelop for details )

3. Save option is not yet implemented you may design structure of project file (XML) and its content files.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
Generalload an sriptfile <test.vb> to the scriptcontrol</test.vb> Pin
Werner Gall4-Feb-10 4:11
Werner Gall4-Feb-10 4:11 
GeneralSupport for debugger & break points Pin
steenhother6-Aug-09 0:00
steenhother6-Aug-09 0:00 
GeneralUsing Microsoft Intellisense. Pin
LKSJDFLKJSDFLKJSD15-Jun-09 23:16
LKSJDFLKJSDFLKJSD15-Jun-09 23:16 
GeneralOne strange problem and ask you your hint for it! Pin
Seraph_summer27-Apr-09 20:58
Seraph_summer27-Apr-09 20:58 
GeneralRe: One strange problem and ask you your hint for it! Pin
Willy Kimura8-Mar-15 4:59
professionalWilly Kimura8-Mar-15 4:59 
QuestionVS2008 Pin
battlemodetwo24-Apr-09 7:28
battlemodetwo24-Apr-09 7:28 
GeneralThank Pin
Bassam Alugili24-Apr-09 6:07
Bassam Alugili24-Apr-09 6:07 
GeneralWell. Pin
Gowdhaman.R10-Feb-09 21:17
Gowdhaman.R10-Feb-09 21:17 
Dear
I develop the javascript editor in c#. Please send answer to my query?
How to set expand and collapse in script when i press { }, - +

I except this one quick
GeneralIf you use any other opensource sourcecode alteast you can metion it. donot publish it undery ou name Pin
affan_198014-Jan-09 19:18
affan_198014-Jan-09 19:18 
GeneralRe: If you use any other opensource sourcecode alteast you can metion it. donot publish it undery ou name Pin
Member 48046902-Jun-09 20:10
Member 48046902-Jun-09 20:10 
Generalgood article and very nice control Pin
pita200018-Nov-08 2:16
pita200018-Nov-08 2:16 
GeneralForms Pin
J Boden13-Nov-08 9:44
J Boden13-Nov-08 9:44 
QuestionWill there be new versions in future? Pin
Jonathan3213-Nov-08 6:29
Jonathan3213-Nov-08 6:29 
AnswerRe: Will there be new versions in future? Pin
Member 48046902-Jun-09 20:14
Member 48046902-Jun-09 20:14 
GeneralStrange exception handling PinPopular
Sergey Morenko13-Jul-08 23:58
professionalSergey Morenko13-Jul-08 23:58 
GeneralRe: Strange exception handling Pin
Rayan Isran4-Dec-11 1:19
Rayan Isran4-Dec-11 1:19 
GeneralDockPanel Suite Copyright Pin
Paul Selormey13-Jul-08 21:48
Paul Selormey13-Jul-08 21:48 
GeneralArticle Width Pin
Paul Selormey13-Jul-08 18:16
Paul Selormey13-Jul-08 18:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.