Skip to content
Jing edited this page Jul 25, 2019 · 4 revisions

CLR Event Binding allows script to bind an .NET event using anonymous function.

Prerequisite

Since ReoScript need to access the .NET object before binding an event. The WorkMode of ScriptRunningMachine must be set to enable AllowDirectAccess and AllowCLREventBind.

ScriptRunningMachine srm = new ScriptRunningMachine();
srm.WorkMode |= MachineWorkMode.EnableDirectAccess | MachineWorkMode.AllowCLREventBind;

See more about WorkMode.

Attach Event

To attach an event of an .NET object, the following syntax can be used:

object.event = function;

For example:

form.load = function() { alert('form loaded.'); };

Now the event 'load' of 'form' has been attached. Once the 'load' be fired in .NET program, the function in script will be called automatically.

Note: Only the public event declared in .NET can be bound in script.

Walkthrough

Create LinkLabel and put it on form in VisualStudio form editor:

CLREventBinding

Change it's name to 'link' and put it into script:

srm.SetGlobalVariable("link", link);

Now bind 'click' event in script:

link.click = function() {
    alert('Link Clicked!');
};

Run this program, and click the LinkLabel, the event fired as shows:

CLR Event Fired

Detach Event

To detach/remove an event binding, we can just set the property to null:

object.event = null;

See Also