Script-defined event handlers. paxScript demo application written in C#.

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using PaxScript.Net;

namespace Hello
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private PaxScripter scripter = new PaxScripter();
	
		public Form1()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			//
			// button1
			//
			this.button1.Location = new System.Drawing.Point(48, 32);
			this.button1.Text = "Run script";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			//
			// Form1
			//
			this.ClientSize = new System.Drawing.Size(210, 88);
			this.Controls.Add(this.button1);
			this.Text = "EventHandlingApp";

		}
		#endregion

		static void Main()
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			scripter.Reset();

			scripter.RegisterType(typeof(Form));
			scripter.RegisterType(typeof(Button));
			scripter.RegisterType(typeof(MessageBox));

			scripter.AddModule("1");

			scripter.AddCode("1", @"using System.Windows.Forms;");

			scripter.AddCode("1", @"public class TestClass");
			scripter.AddCode("1", @"{");
			scripter.AddCode("1", @"public static void OnClickHandler(object sender, EventArgs e)");
			scripter.AddCode("1", @"{");
			scripter.AddCode("1", @"MessageBox.Show(sender.ToString());");
                        scripter.AddCode("1", @"MessageBox.Show(e.ToString());");
			scripter.AddCode("1", @"}");
			scripter.AddCode("1", @"}");

			scripter.AddCode("1", @"Form f = new Form();");
			scripter.AddCode("1", @"f.Text = ""Script-defined application"";");
			scripter.AddCode("1", @"Button b = new Button();");
			scripter.AddCode("1", @"b.Text = ""Click me"";");
			scripter.AddCode("1", @"b.Click += new EventHandler(TestClass.OnClickHandler);");

			scripter.AddCode("1", @"f.Controls.Add(b);");

			scripter.AddCode("1", @"f.ShowDialog();");

			scripter.Run(RunMode.Run);
			if (scripter.HasErrors)
				MessageBox.Show(scripter.Error_List[0].Message);
		}
	}
}

Note

You have to register host-defined event handlers explictly. 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 without any changes.


Copyright © 1999-2017 Alexander Baranovsky. All rights reserved.