Click here to Skip to main content
15,868,016 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.4K   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

 
Questionis it opensource controls ? Pin
Member 1454961926-Nov-19 19:36
Member 1454961926-Nov-19 19:36 
QuestionPlease Convert C# to VB.Net Pin
Member 1308285311-Jul-17 10:02
Member 1308285311-Jul-17 10:02 
QuestionComplete code for the NET component Pin
Member 99805024-Jul-17 2:50
professionalMember 99805024-Jul-17 2:50 
QuestionIt throws an exception on start Pin
Aytek Ustundag26-Jun-17 17:52
Aytek Ustundag26-Jun-17 17:52 
AnswerRe: It throws an exception on start Pin
Member 99805024-Jul-17 2:40
professionalMember 99805024-Jul-17 2:40 
QuestionHow to modify the code for cpp programming ? Pin
Vinay Teja Reddy28-Jun-15 20:33
Vinay Teja Reddy28-Jun-15 20:33 
GeneralExecute Script with Parameters Pin
Member 846167928-Jan-15 6:17
Member 846167928-Jan-15 6:17 
QuestionInput Chinese word Pin
Member 1032216619-Oct-13 15:01
Member 1032216619-Oct-13 15:01 
QuestionCan you please send me code sample for breakpoint? Pin
Elegiac13-Jun-13 22:56
Elegiac13-Jun-13 22:56 
GeneralMy vote of 5 Pin
SunilGupta04855-Jun-13 3:54
professionalSunilGupta04855-Jun-13 3:54 
GeneralMy vote of 5 Pin
Sergey221014-Apr-13 8:38
Sergey221014-Apr-13 8:38 
Questiondebugger Pin
salihovic5-Mar-13 23:12
salihovic5-Mar-13 23:12 
GeneralMy vote of 5 Pin
Ramez Ashraf30-Jan-13 7:57
Ramez Ashraf30-Jan-13 7:57 
QuestionHow do i use codeeditor control with syntax highlight and intellisense for java? Pin
Hem sharma Acharya11-Sep-12 23:57
Hem sharma Acharya11-Sep-12 23:57 
GeneralMy vote of 5 Pin
mohammad dianati9-Oct-11 20:48
mohammad dianati9-Oct-11 20:48 
GeneralRe: My vote of 5 Pin
Rayan Isran4-Dec-11 1:27
Rayan Isran4-Dec-11 1:27 
GeneralJavaScript = Java language Auto Complete and highlighting Pin
Melih_123_melih17-Mar-11 23:02
Melih_123_melih17-Mar-11 23:02 
GeneralJavaScript and XML Pin
Melih_123_melih17-Mar-11 22:07
Melih_123_melih17-Mar-11 22:07 
GeneralMy vote of 1 Pin
gcelik30-Nov-10 1:58
gcelik30-Nov-10 1:58 
GeneralMy vote of 1 Pin
undisclosedlol17-Jun-10 13:37
undisclosedlol17-Jun-10 13:37 
GeneralMy vote of 1 Pin
Krunal Mevada14-Apr-10 2:30
Krunal Mevada14-Apr-10 2:30 
GeneralRe: My vote of 1 Pin
undisclosedlol17-Jun-10 13:37
undisclosedlol17-Jun-10 13:37 
GeneralMy vote of 2 Pin
abdurahman ibn hattab2-Mar-10 10:32
abdurahman ibn hattab2-Mar-10 10:32 
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 

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.