Click here to Skip to main content
Click here to Skip to main content

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

By , 13 Jul 2008
 

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.

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.

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 Irun interface for cross appdomain communication.

            

#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)

About the Author

Rajneesh Noonia
Software Developer (Senior)
United Kingdom United Kingdom
Member
I am an Techinical Lead working with Seria India Ltd. (Previously Known as Xansa),Noida, India. I have worked with Microsoft Visual Basic 6.0 , SQL Server,COM ,WIN32 API. I have been playing with the .NET Framework since it was in beta but have been using it commercially since late 2004.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberSergey221014 Apr '13 - 8:38 
Questiondebuggermembersalihovic5 Mar '13 - 23:12 
GeneralMy vote of 5memberRamez Ashraf30 Jan '13 - 7:57 
QuestionHow do i use codeeditor control with syntax highlight and intellisense for java?memberHem sharma Acharya11 Sep '12 - 23:57 
GeneralMy vote of 5membermohamad dianaty9 Oct '11 - 20:48 
GeneralRe: My vote of 5memberleosco4 Dec '11 - 1:27 
GeneralJavaScript = Java language Auto Complete and highlightingmemberMelih_123_melih17 Mar '11 - 23:02 
GeneralJavaScript and XMLmemberMelih_123_melih17 Mar '11 - 22:07 
GeneralMy vote of 1membergcelik30 Nov '10 - 1:58 
GeneralMy vote of 1memberundisclosedlol17 Jun '10 - 13:37 
GeneralMy vote of 1memberKrunal Mevada14 Apr '10 - 2:30 
GeneralRe: My vote of 1memberundisclosedlol17 Jun '10 - 13:37 
GeneralMy vote of 2memberabdurahman ibn hattab2 Mar '10 - 10:32 
Generalload an sriptfile <test.vb> to the scriptcontrol</test.vb>memberWerner Gall4 Feb '10 - 4:11 
GeneralSupport for debugger & break pointsmembersteenhother6 Aug '09 - 0:00 
GeneralUsing Microsoft Intellisense.memberMr. Badr CHOUFFAI15 Jun '09 - 23:16 
GeneralOne strange problem and ask you your hint for it!memberSeraph_summer27 Apr '09 - 20:58 
QuestionVS2008memberbattlemodetwo24 Apr '09 - 7:28 
GeneralThankmemberBassam Alugili24 Apr '09 - 6:07 
GeneralWell.membergowdhamanmca@gmail.com10 Feb '09 - 21:17 
GeneralIf you use any other opensource sourcecode alteast you can metion it. donot publish it undery ou namememberaffan_198014 Jan '09 - 19:18 
GeneralRe: If you use any other opensource sourcecode alteast you can metion it. donot publish it undery ou namememberMember 48046902 Jun '09 - 20:10 
Generalgood article and very nice controlmemberpita200018 Nov '08 - 2:16 
GeneralFormsmemberJ Boden13 Nov '08 - 9:44 
QuestionWill there be new versions in future?memberJonathan3213 Nov '08 - 6:29 
AnswerRe: Will there be new versions in future?memberMember 48046902 Jun '09 - 20:14 
GeneralStrange exception handlingmemberGSerjo13 Jul '08 - 23:58 
GeneralRe: Strange exception handlingmemberleosco4 Dec '11 - 1:19 
GeneralDockPanel Suite CopyrightmemberPaul Selormey13 Jul '08 - 21:48 
GeneralArticle WidthmemberPaul Selormey13 Jul '08 - 18:16 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 13 Jul 2008
Article Copyright 2008 by Rajneesh Noonia
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid