Script-defined event handlers. paxScript demo application written in VB.NET.

Imports PaxScript.Net

Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend scripter As PaxScripter = New PaxScripter

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        scripter.AddModule("1", "VB")

        scripter.AddCodeLine("1", "Imports System.Windows.Forms")

        scripter.AddCodeLine("1", "Public Class TestClass")
        scripter.AddCodeLine("1", "Public Shared Sub OnClickHandler(ByRef Sender As Object, ByRef e As EventArgs)")
        scripter.AddCodeLine("1", "MessageBox.Show(sender.ToString())")
        scripter.AddCodeLine("1", "MessageBox.Show(e.ToString())")
        scripter.AddCodeLine("1", "End Sub")
        scripter.AddCodeLine("1", "End Class")

        scripter.AddCodeLine("1", "Dim f As Form = New Form()")
        scripter.AddCodeLine("1", "f.Text = ""Script-defined application""")
        scripter.AddCodeLine("1", "Dim b As Button = new Button()")
        scripter.AddCodeLine("1", "b.Text = ""Click me""")
        scripter.AddCodeLine("1", "AddHandler b.Click, AddressOf TestClass.OnClickHandler")

        scripter.AddCodeLine("1", "f.Controls.Add(b)")

        scripter.AddCodeLine("1", "f.ShowDialog()")

        If (scripter.HasErrors) Then
        End If
    End Sub
End Class


paxScript.NET uses System.Reflection.Emit to generate a wrapper method for script-defined event handler. This approach is not suitable under .NET CF because .NET CF currently does not include System.Reflection.Emit namespace. Therefore you have to register host-defined events explictly under .NET CF. For example
scripter.RegisterEventHandler(typeof(MyEventHandler), "MyEvent", new MyEventHandler(Helper));
where Helper is a host-defined method which calls script-defined delegate. For example:
  void Helper(object sender, MyEventArgs x)
       scripter.ApplyDelegate("MyEvent", sender, x); // the first 2 arguments will determine the script-defined delegate
The following events of System.Windows.Forms.Control class are already registered:

Click CursorChanged DockChanged DoubleClick EnabledChanged Enter Leave MouseEnter MouseLeave Resize TextChanged
so, the demo application presented at this page will work correctly under .NET CF without any changes.

Copyright © 2005-2022 Alexander Baranovsky. All rights reserved.