This topic takes the form of a tutorial that describes how to create a C# form. The form is linked to a Fortran DLL that carries out an elementary processing task.
The approach described here has now been subsumed into Visual ClearWin (see Getting started for a set of Visual ClearWin samples).
First we create a new project by selecting New from the main File menu. From the Project types select "Visual C# Projects" and then from the Templates select "Windows Application". The default name will be WindowApplication1.
You will be presented with a blank form called Form1 in design view. Now use the Toolbox to add some components to this form. If the Toolbox is not visible on the left hand margin then select Toolbox from the main View menu.
The Toolbox appears when the mouse cursor hovers over the Toolbox bar and is automatically hidden when the mouse is moved away from the Toolbox. Click on the pin icon at the top of the Toolbox to switch off Auto Hide.
We will create a form that looks like this:
The form contains two Labels for the text "Input" and "Output", two Textboxes for the input and output fields and a Button that will be linked to a Fortran procedure that converts the input to the output.
Take care to create the Textbox for the input first because the first control automatically has the focus when the form is loaded. If you forget to do this then you should change the TabIndex property for this control to zero.
Click on TextBox in the Toolbox and drag a rectangle with a suitable size and position on the form. Keep the default name textBox1.
Do the same again to create textBox2 for the output. Use the Properties window to make this ReadOnly.
Click on Button in the Toolbox and drag a rectangle for the button. Keep the default name button1 but, in the Properties window, change the text to "Process".
Click on Label in the Toolbox and drag a rectangle first for the text "Input" and repeat for "Output".
When the form is loaded the user will enter a number as input and then click on the button in order to view the result of some calculation. The processing will take place in a Fortran DLL. First we enter code to respond to a button click. Later we will consider out how to prevent the user from typing invalid characters into the input box.
Using the design view of the form, double click on the button. This switches to the code view for the form and plants code for a function called button1_Click (as well as what you can see, code for button1_Click is also added in the "Windows Form Designer generated code" region).
Now add the following code as the body of the function button1_Click.
if(Double.TryParse(textBox1.Text, System.Globalization.NumberStyles.Any, null, out v))
textBox2.Text = Project1.Process(v).ToString();
else textBox2.Text = "Error";
Project1.Process has not been written yet so the code will not compile.
Now we need to add a new project to the solution. This project is a Fortran DLL called Project1 containing a function called Process.
From the main File menu, select New . Now from the Project types select "FTN95 Projects" and the from the Templates select "FTN95 .NET project". Use the default name Project1.
Right click on Project1 in the Solution Explorer window and select Properties from the popup menu. In the dialog box for Project1 Property Pages, change the Output File Type to DLL. Close this dialog.
Right click again on Project1 in the Solution Explorer window and now select Add and then Add New Item. Select "FTN95 free format" source file in the resulting dialog box and use the default file name (freeformat1.f95).
Copy the following code into this file.
The use of the INTENT(IN) attribute for x is not essential.
The ASSEMBLY_INTERFACE serves two purposes. a) It means we can use "Process" rather than "PROCESS" as the C# name for the function and b) it means that the argument x is passed by value (the alternative is to use a pointer when calling Process from C# but to do this we would have to make the C# code "unsafe").
Build the DLL.
The final step is to add a reference to the DLL. Right click on the WindowsApplication1 project in the Solutions Explorer window and select Add Reference. In the Add References dialog, select the Projects tab and then select Project1.
Finally build the solution and run the application (press F5).
At the moment the user is allowed to type any character string into the input box and it would be better if the input were restricted to valid floating point numbers. In order to add code to filter the input, go back to the design view of the form and double click on the input Textbox. This switches to the code view and adds outline code for a function called textBox1_TextChanged. You can now add code to this function in order to filter the user input. For further information on C# forms see the Samples 2 and and Sample 6 in Installed Samples and also Windows Forms.
It is possible to create user controls that inherit attributes and methods from standard controls such as the TextBox control. These emulate features from ClearWin+ that are relevant to this context. For example, there are controls called Double_Box and Int32_Box that are derived from TextBox and that automatically filter the input for floating point values and integer values respectively.
To use a Double_Box instead of a TextBox for input, first use the design view of the form to select and delete textBox1. Now right click on the Toolbox and select Customise Toolbox. Select the .NET Framework Components tab and click on the NameSpace column header to find Salford.Windows.Forms. Click on the check box for Double_Box. When you close the customising dialog, you will see the new control in the toolbox. (Note that you will only need to customise the toolbox once and also that you can use the Toolbox right-click popup menu to create a new tab in the Toolbox for Silverfrost controls.) Select a DoubleBox from the Toolbox and drag a rectangle on to the form. By default this will have the name double_Box1.
Now view the properties of double_Box1 and note the additional properties that appear in the Data category. These are AutoCorrect, ExpFigs, Maximum, Minimum and Sigfigs. Select each in turn and note the description in the Descriptions pane.
Change the body of the function button1_Click to:
if(doubleBox1.ioStat==0) textBox2.Text =
else textBox2.Text = "";
Finally rebuild the solution and run the application (press F5). Note the behaviour using both valid and invalid data entries. Run the application again using range checking (setting values for Maximum and Minimum in the Properties Window) and a using different value for AutoCorrect.
When the program is run with AutoCorrect set to true, an invalid input (such as the unfinished entry "1.0e"), generates a warning when the button is clicked. The warning takes the form of a warning style icon that appears to the right of the input box. When the mouse hovers over the icon, a tooltip appears that displays the warning. A different warning message indicates an entry that was out of range.
A similar effect is obtained when AutoCorrect is set to false but when the button is clicked:
a) an invalid input value is not automatically corrected,
b) the code must use the ioStat attribute to test if the input is valid, and
c) when the input is invalid a flashing error style icon appears.
The possible values of ioStat are:
0 No error.
1 An invalid value.
2 The value is less than the prescribed minimum.
3 The value is greater than the prescribed maximum.
If AutoCorrect is set to true, once the focus is lost (i.e. a control other than the input box has the focus), ioStat will aways be zero because any invalid value will have been corrected.