Introduction
Many modern applications support script functions that provide the user a way to programmatically control the application itself. This technology lets the user have the ability to control the flow of the program as they wish. Before, Microsoft had provided IActiveScript
interface to access the script engine. In .NET framework, Microsoft provides many classes to support script engine, please refer to ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfMicrosoftVsa.htm. I have written a sample program to show how to do it in C#. In this sample program, I created a winform control and put it in my sample program, later I will show how to use VB script to call method in this program, and handle the event from this control using the script. The user control is named UserControl1, it has one method and one event.
You must implement Microsoft.Vsa.IVsaSite
, it provides a bridge between your program and script engine
public class TestVsaSite : Microsoft.Vsa.IVsaSite
{
private System.Collections.Hashtable eventInstance;
public TestVsaSite()
{
eventInstance = new Hashtable();
}
public void AddEvent(string name, string type, object instance,
bool events)
{
eventInstance.Add(name, instance);
}
public void GetCompiledState(out byte[] pe, out byte[] debugInfo)
{
}
public object GetEventSourceInstance(string itemName,
string eventSourceName)
{
return eventInstance[eventSourceName];
}
public object GetGlobalInstance(string name)
{
return (object)null;
}
public void Notify(string notify, object info)
{
}
public bool OnCompilerError(IVsaError error)
{
return false;
}
}
Now create a script engine instance
m_vbEngine = new Microsoft.VisualBasic.Vsa.VsaEngine();
You can hook the event by calling
m_vbScript.AddEventSource(name, type);
m_vbTempRef = (IVsaReferenceItem)m_vbItems.CreateItem(name,
VsaItemType.Reference, VsaItemFlag.None);
Here is the VB script sample
imports myNotClass
imports System
Public Module Vespu
#Region "Automatically generated code, do not modify"
<System.ContextStaticAttribute()> Public WithEvents ctrlInScript _
As myNotClass.UserControl1
#End Region
Sub main
End Sub
Sub ctrlInScript_tick(count As Integer) Handles ctrlInScript.tick
dim disp as string
disp = "tick counter is: " & count.ToString()
ctrlInScript.SetText(disp)
End Sub
End Module