An example of calling .NET from APLX
The following complete example shows a simple application which displays a window with an edit box, a text box to output results, and a button. When the button is clicked, it simply evaluates the APL expression typed into the edit box and displays the result in the text box.
(Note that this example is intended to show the use of .NET classes from APLX. You could also write this example using APLX's built-in classes for GUI programming, which would have the advantage that the result would work on all platforms which support APLX - Windows, Macintosh and Linux).
In the following function listing, note in particular the callbacks set up on lines 32 and 33. The first of these (the Closed event) is triggered when the window is closed; this terminates the function by clearing the )SI, and this in turn causes all the object references to be deleted because they are held in localized variables. The second call back (the Click event of the button) causes the EVALUATE function to be run.
∇DOT_Forms;X;F;EditResult;EditIn;ButtonDo
[1] ⍝ Simple example of using Windows Forms from APLX v4
[2] ⍝ Create main form
[3] F←'.net' ⎕NEW 'Form'
[4] F.Text←'Expression Evaluator'
[5] ⍝
[6] ⍝ Create edit box for input line
[7] EditIn←'.net' ⎕NEW 'TextBox'
[8] EditIn.Left←12 ⋄ EditIn.Top←21
[9] EditIn.Width←265 ⋄ EditIn.Height←20
[10] EditIn.Font←'.net' ⎕NEW 'Font' 'APLX Upright' 10
[11] F.Controls.Add EditIn
[12] ⍝
[13] ⍝ Create multi-line box for result
[14] EditResult←'.net' ⎕NEW 'TextBox'
[15] EditResult.Multiline←1
[16] EditResult.Left←12 ⋄ EditResult.Top←50
[17] EditResult.Width←265 ⋄ EditResult.Height←180
[18] EditResult.Font←'.net' ⎕NEW 'Font' 'APLX Upright' 10
[19] F.Controls.Add EditResult
[20] ⍝
[21] ⍝ Create button
[22] ButtonDo←'.net' ⎕NEW 'Button'
[23] ButtonDo.Left←94 ⋄ ButtonDo.Top←240
[24] ButtonDo.Width←100 ⋄ ButtonDo.Height←22
[25] ButtonDo.Text←'Evaluate'
[26] F.Controls.Add ButtonDo
[27] ⍝
[28] ⍝ Make the button accept Enter as equivalent to clicking
[29] F.AcceptButton←ButtonDo
[30] ⍝
[31] ⍝ Add callbacks
[32] F.Closed←'"Cleaning up.." ⋄ →'
[33] ButtonDo.Click←'EditResult EVALUATE EditIn.Text'
[34] ⍝
[35] ⍝ Show the window
[36] F.Show
[37] ⍝
[38] ⍝ Process events
[39] X←⎕WE ¯1
∇ Here is the callback function which runs when the 'Evaluate' button is clicked:
∇A EVALUATE B;RESULT;⎕IO
[1] ⍝ Evaluate expression B and put it into the Text property of object A
[2] ⍝ If an error occurs, change the colour of the text
[3] ⎕IO←1
[4] RESULT←⎕EC B
[5] :Select ↑RESULT
[6] :Case 0 ⍝ Error
[7] A.ForeColor←A.ForeColor.Red
[8] A.Text←MAKEVEC 3⊃RESULT
[9] :Case 1 ⍝ Expression with a result which would display
[10] A.ForeColor←A.ForeColor.DarkOliveGreen
[11] A.Text←MAKEVEC⍕3⊃RESULT
[12] :Else
[13] ⍝ 2 Expression with a result which would not display
[14] ⍝ 3 Expression with no explicit result
[15] ⍝ 4 Branch to a line
[16] ⍝ 5 Naked branch
[17] A.Text←''
[18] :End
∇ Finally, here's a small support function used by the above:
∇R←MAKEVEC B
[1] ⍝ Given a character array, ensure it's a text vector
[2] ⍝ If matrix or higher rank, make into CRLF-delimited vector
[3] :If 2>⍴⍴B
[4] R←B
[5] :Else
[6] R←⎕R ⎕BOX B
[7] :EndIf
[8] R←⎕SS R ⎕R(⎕R,⎕L)
∇
Using Windows Presentation Foundation (WPF)
The example above makes use of the .NET framework System.Windows.Forms
Although System.Windows.Forms is still the most widespread .NET UI framework in use today, Microsoft now encourage programmers to make use of an alternative known as Windows Presentation Foundation (WPF). This has a number of advantages, including the use of a markup language known as XAML to describe the appearance of the UI. XAML allows the programmer to create a rich UI with comparatively little effort.
For more information about using WPF and XAML from APLX, see this web page. Note that the page includes a sample workspace which contains a modified version of the code listed above. In the modified version, the UI is described through XAML markup, and APL just has to handle events.
Further .NET examples using APLX
See here for other examples of using .NET classes from APLX.
Author:SimonMarsden CategoryAplx CategoryDotNet
APL Wiki