Skip to content
Jing Lu edited this page May 16, 2013 · 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 to ScriptRunningMachine must be changed 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 could be used:

object.event = function;

For example: (does it look very JavaScript? :-))

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 in script automatically.

Note: Only the public event declared in .Net could 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](http://www.unvell.com/reoscript/images/topic_link_clicked.png)

Detach Event

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

object.event = null;

See Also

Clone this wiki locally