PaxScripter.Invoke demo application written in C#.

demo_invoke.jpg
unit WinForm1;

interface

uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data, PaxScript.Net;

type
  TWinForm1 = class(System.Windows.Forms.Form)
  {$REGION 'Designer Managed Code'}
  strict private
    components: System.ComponentModel.IContainer;
    PaxScripter1: PaxScript.Net.PaxScripter;
    btnReset: System.Windows.Forms.Button;
    btnCallMethod: System.Windows.Forms.Button;
    tbState: System.Windows.Forms.TextBox;
    lblState: System.Windows.Forms.Label;
    rtbScript: System.Windows.Forms.RichTextBox;
    procedure InitializeComponent;
    procedure btnReset_Click(sender: System.Object; e: System.EventArgs);
    procedure btnCallMethod_Click(sender: System.Object; e: System.EventArgs);
    procedure PaxScripter1_OnChangeState(sender: PaxScript.Net.PaxScripter; e: PaxScript.Net.ChangeStateEventArgs);
  {$ENDREGION}
  strict protected
    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;

  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm1))]

implementation

{$REGION 'Windows Form Designer generated code'}
procedure TWinForm1.InitializeComponent;
begin
  Self.components := System.ComponentModel.Container.Create;
  Self.PaxScripter1 := PaxScript.Net.PaxScripter.Create(Self.components);
  Self.btnReset := System.Windows.Forms.Button.Create;
  Self.btnCallMethod := System.Windows.Forms.Button.Create;
  Self.tbState := System.Windows.Forms.TextBox.Create;
  Self.lblState := System.Windows.Forms.Label.Create;
  Self.rtbScript := System.Windows.Forms.RichTextBox.Create;
  Self.SuspendLayout;
  //
  // PaxScripter1
  //
  Include(Self.PaxScripter1.OnChangeState, Self.PaxScripter1_OnChangeState);
  //
  // btnReset
  //
  Self.btnReset.Location := System.Drawing.Point.Create(24, 24);
  Self.btnReset.Name := 'btnReset';
  Self.btnReset.Size := System.Drawing.Size.Create(96, 23);
  Self.btnReset.TabIndex := 0;
  Self.btnReset.Text := 'Reset';
  Include(Self.btnReset.Click, Self.btnReset_Click);
  //
  // btnCallMethod
  //
  Self.btnCallMethod.Location := System.Drawing.Point.Create(24, 56);
  Self.btnCallMethod.Name := 'btnCallMethod';
  Self.btnCallMethod.Size := System.Drawing.Size.Create(96, 23);
  Self.btnCallMethod.TabIndex := 1;
  Self.btnCallMethod.Text := 'Call Method';
  Include(Self.btnCallMethod.Click, Self.btnCallMethod_Click);
  //
  // tbState
  //
  Self.tbState.Location := System.Drawing.Point.Create(24, 128);
  Self.tbState.Name := 'tbState';
  Self.tbState.TabIndex := 2;
  Self.tbState.Text := '';
  //
  // lblState
  //
  Self.lblState.Location := System.Drawing.Point.Create(24, 96);
  Self.lblState.Name := 'lblState';
  Self.lblState.TabIndex := 3;
  Self.lblState.Text := 'Scripter state';
  //
  // rtbScript
  //
  Self.rtbScript.Location := System.Drawing.Point.Create(152, 16);
  Self.rtbScript.Name := 'rtbScript';
  Self.rtbScript.Size := System.Drawing.Size.Create(304, 144);
  Self.rtbScript.TabIndex := 4;
  Self.rtbScript.Text := 'using System;'#10'public class Demo'#10'{'#10#9'public static int X = 0;'#10#9'public static int '+
  'Increase()'#10#9'{'#10#9#9'return ++X;'#10#9'}'#10#9'public static void Main(){}'#10'}';
  //
  // TWinForm1
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(472, 174);
  Self.Controls.Add(Self.rtbScript);
  Self.Controls.Add(Self.lblState);
  Self.Controls.Add(Self.tbState);
  Self.Controls.Add(Self.btnCallMethod);
  Self.Controls.Add(Self.btnReset);
  Self.Name := 'TWinForm1';
  Self.Text := 'CallMethodApp';
  Self.ResumeLayout(False);
end;
{$ENDREGION}

procedure TWinForm1.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;

constructor TWinForm1.Create;
begin
  inherited Create;
  InitializeComponent;
  tbState.Text := 'Init';
end;

procedure TWinForm1.PaxScripter1_OnChangeState(sender: PaxScript.Net.PaxScripter;
  e: PaxScript.Net.ChangeStateEventArgs);
begin
  case e.NewState of
    ScripterState.Init: tbState.Text := 'Init';
    ScripterState.Terminated: tbState.Text := 'Terminated';
  end;

  if e.OldState = ScripterState.Init then
  begin
    sender.AddModule('1');
    sender.AddCode('1', rtbScript.Text);
  end
  else if sender.HasErrors then
    MessageBox.Show(sender.Error_List[0].Message);
end;

procedure TWinForm1.btnCallMethod_Click(sender: System.Object; e: System.EventArgs);
var
  result: System.Object;
begin
  result := paxScripter1.Invoke(RunMode.Run, nil, 'Demo.Increase', []);
  MessageBox.Show('X = ' + result.ToString());
end;

procedure TWinForm1.btnReset_Click(sender: System.Object; e: System.EventArgs);
begin
  paxScripter1.Reset();
end;

end.


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