Theme:
v
APL+Win 13.2 ⎕CSE Tutorial (5)

In this example we will create an APL ⎕CSE script that uses a C# DLL to display the same C# Form as in the previous example.

To reproduce this example, you will need to download and install Visual Studio Express 2012 (a free download from the Microsoft Web Site at: http://www.microsoft.com/visualstudio/eng/downloads, selecting either Visual Studio Express 2012 for Windows 8 or Visual Studio Express 2012 for Windows Desktop depending on your version of Windows)

Example 12: Create and use a Windows Form from a C# DLL

Let's create a C# DLL that contains the same C# form as we have been developing in the previous example.

Here is how to proceed:

  1. Start Visual Studio 2012 (or Visual Studio Express 2012)
    In this example I'll be using Visual Studio 2012 Premium, but the same can be done with Visual Studio Express 2012.
  2. Select File / New / Project...
  3. In the New Project dialog, be sure that .Net Framework 4.5 is selected in the top Combo Box
    Select the Windows Forms Application template
    Enter InterestCalc for the Project Name:
    In the Location: field, select a folder to save this project to
    Keep the Create directory for solution check box checked

    The New Project VS2012 dialog
  4. Click OK
    Note that creating a Windows Forms Application project will create a .exe file, not a .dll file. However it is easy to change that later.
    The following Form gets displayed in Design Mode:

    The InterestCalc Form

    In the Toolbox, on the left, expand Common Controls (if the Toolbox is not visible, select VIEW / Toolbox)
  5. In the Properties pane at the bottom right of the screen:
    change the Text property to Interest Calculator
    change the Size property to 400,300 (note that in the .Net Framework the Width dimension comes first)
    change the MinimumSize property to 400,300 as well
    change the StartPosition property to CenterScreen
  6. Drag and drop a Label control to the Form client area from the Toolbox
    Change its Name property to lPrincipal Change its Text property to Principal: Then move it with your mouse till the little blue guide lines get displayed, as in:

    Positioning a Label on the Form
  7. Drag and drop a TextBox control to the Form client area from the Toolbox
    Change its Name property to txtPrincipal (note: the (Name) property is at the top of the properties list)
    Then, move it with your mouse till the little blue and pink guide lines get displayed, as in:

    Positioning a TextBox on the Form

    Then, change its Size property to 140,20
  8. Proceed as in the last 2 steps to add the following new controls to the form:
    Warning: when you change a property for a control, be SURE to have selected the control with your mouse first
    Add a new Label, change its Name to lInterest and change its Text to Interest:
    Add a new TextBox, change its Name to txtInterest and change its Size to 140,20
    Add a new Label, change its Name to lYears and change its Text to Years:
    Add a new NumericUpDown, change its Name to nudYears, change its Size to 140,20, change its Minimum to 5 and its Maximum to 50
    Add a new Label, change its Name to lBalance and change its Text to Yearly Account Balance:
    Add a new TextBox, change its Name to txtBalance, change its Multiline property to true and change its ReadOnly property to true
    Grab the bottom right corner of the txtBalance TextBox and resize it so that it uses the whole bottom part of the Form
    Add a new Button, change its Name to btnCalc, its Text to Calculate and its Size to 90,22
    Adjust all the controls with your mouse until the little blue or pink guide lines help you know they are correctly aligned or at the right suggested position until you get a User Interface similar to the following:

    The complete InterestCalc Form User Interface
  9. Now adjust the controls Anchor properties as follows to be sure the Form will resize nicely:
    select the txtPrincipal, txtInterest and nudYears controls (by clicking the first one then Ctrl+clicking the other 2)
    change their Anchor property to Top,Left,Right
    select the btnCalc button and change its Anchor property to Top, Right select the txtBalance TextBox and change its Anchor property to Top, Right, Bottom, Left
  10. Try to run the application by clicking the Start toolbar button
    Try resizing the form and check that it behaves as you expect
  11. In Solution Explorer (the top right pane), right click Form1.cs and select View Code or press F7 to view the C# code associated with the form
    You should see something like:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace InterestCalc
    {
    	public partial class Form1 : Form
    	{
    		public Form1()
    		{
    			InitializeComponent();
    		}
    	}
    }
    
    Note that the remaining part of the C# code (the one corresponding to the User Interface) is in file Form1.Designer.cs which you are not supposed to open and change yourself, although you may if you really wanted to.
  12. To create a Click event handler for the btnCalc button, just double click the btlCalc button
    This should automatically update the Form1.cs code to read:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace InterestCalc
    {
    	public partial class Form1 : Form
    	{
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		private void btnCalc_Click(object sender, EventArgs e)
    		{
    
    		}
    	}
    }
    
  13. If you have named all the controls as suggested above, you can then copy/paste the btnCalc_Click event handler code we wrote in the previous example, so that Form1.cs now reads:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace InterestCalc
    {
    	public partial class Form1 : Form
    	{
    		public Form1()
    		{
    			InitializeComponent();
    		}
    
    		private void btnCalc_Click(object sender, EventArgs e)
    		{
    			double balance;
    			double principal = Convert.ToDouble(txtPrincipal.Text);
    			double interest = Convert.ToDouble(txtInterest.Text);
    			int years = Convert.ToInt32(nudYears.Value);
    			string text = "Year\tBalance\r\n";
    			for (int i = 1; i <= years; i++)
    			{
    				balance = principal * Math.Pow((100 + interest) / 100, i);
    				text += string.Format("{0}\t{1:C}\r\n", i, balance);
    			}
    			txtBalance.Text = text;
    		}
    	}
    }
    
  14. In order to be complete, we need to fix the Tab order in the form
    For that, click on the form caption in Design Mode
    Then select VIEW / Tab Order
    Then click on each blue square from top left to bottom right, finishing with btnCalc

    Positioning a TextBox on the Form

    Then select VIEW / Tab Order once again to get rid of the blue squares showing the Tab order
  15. The C# application is now complete: you can test it by click the toolbar Start button or pressing F5

    Using the InterestCalc C# application
  16. But we would like to use this form from APL+Win
    So the first step is to convert this Windows Forms executable application into a DLL
    This is pretty simple!
  17. In Solution Explorer, double click Properties
    Open the Output type: combo box and select Class Library

    Changing to a Class Library to produce a DLL

    Press Ctrl+S to save your change
    After that, be sure to recompile the application by right clicking InterestCalc in Solution Explorer and by selecting Rebuild (note that you can also simply press F6)
    Note that this will create an InterestCalc.dll in your C# project bin\debug folder
  18. Let's now create an APL+Win script called InterestCalcDLL to use this DLL form
        ∇ InterestCalcDLL;s;z;⎕cself
    [1]
    [2]   ⎕cself←'c'⎕cse'Init' 'System' 'System.Drawing' 'System.Windows.Forms'
    [3]   z←⎕cse'LoadAssembly' 'D:\Documents\Visual Studio 2013\Projects\Test\InterestCalc\InterestCalc\bin\Debug\InterestCalc.dll'
    [4]
    [5]   s←''
    [6]   s←s,⊂'using InterestCalc;'
    [7]   s←s,⊂'using System.Windows.Forms;'
    [8]   s←s,⊂'Form1 form1 = new Form1();'
    [9]   s←s,⊂'form1.ShowDialog();'
    [10]  s←⊃s
    [11]
    [12]  z←⎕cse'Exec's
    [13]  :if z=¯1 ⋄ ⎕error⎕cse'GetLastError' ⋄ :endif
    [14]
        ∇
    

    This time the APL script allowing to use our C# form is extremely simple.

    We load the DLL assembly compiled in Visual Studio 2012.

    We then declare 2 using statements, declaring we will be using InterestCalc and System.Windows.Forms namespaces.

    Then all what remains to be done is to create an instance of the Form1 class and to show it modally with ShowDialog

  19. Let's run the APL InterestCalcDLL script function:
          InterestCalcDLL
    
    Using the InterestCalc C# DLL application from APL

CSE Tutorial (1)...
CSE Tutorial (2)...
CSE Tutorial (3)...
CSE Tutorial (4)...

More examples to come soon!