Adding a Dialog to a Macro

 

If you add a dialog to the macro then the user can easily interact with it. Lets create a dialog that allows user entry of the number of data sets to be fitted. There are two basic steps to do this: 1) creating the dialog visually and 2) writing the code that characterizes the behavior of the dialog.

First create the dialog visually. Open the Exponential Fit macro Edit window, place your cursor after the Sub Main statement as shown below and click on the User Dialog button.

The UserDialog Editor window appears. Click on the AddText button and create a text label by dragging the mouse.

Repeat this procedure to add a textbox next to the text label and OK and Cancel buttons. Move these objects around to obtain a rough placement on the dialog. Then adjust the width and height of the dialog.

Double click the text label. The Edit Text Properties dialog box that is shown on the left below is displayed. Enter the caption “Number of Data Sets”. Interactively edit the position parameters of all objects to achieve precise vertical and horizontal alignment. For example, the Top parameters for the text label (left) and the textbox (right) are both set to be 7.

As a final touch, double click on the dialog design window and enter the dialog caption “Fit Multiple Data Sets”. This results in the final dialog on the left below and the macro code for it on the right.

BEGIN DIALOG USERDIALOG 270,70, "FIT MULTIPLE DATA SETS"
TEXT 10,7,90,28,"NUMBER OF DATSETS",.TEXT1
TEXTBOX 100,7,70,14,.TEXTBOX1
OKBUTTON 190,7,78,21
CANCELBUTTON 190,35,78,21
END DIALOG
DIM DLG AS USERDIALOG
DIALOG DLG

This is the code which displays the dialog. To give the dialog function we add the additional argument “.DialogFunc to the end of the Begin Dialog statement

BEGIN DIALOG USERDIALOG 270,70,"FIT MULTIPLE DATA SETS",.DIALOGFUNC ' % GRID:10,7,1,1

This is the name of the function that determines the behavior of the dialog. We will copy a sample dialog function from Help and paste it into our macro. Highlight “DialogFunc” in the Begin Dialog statement and press F1. Select the DialogFunc Prototype topic. Copy the syntax directly from Help and delete Cases 3, 4, and 5 (we won’t be needing them) and paste it to the bottom of your macro. The following code is added to your macro:

FUNCTION DIALOGFUNC(DLGITEM$, ACTION%, SUPPVALUE%) AS BOOLEAN
SELECT CASE ACTION%
CASE 1 ' DIALOG BOX INITIALIZATION
...
CASE 2 ' VALUE CHANGING OR BUTTON PRESSED
...
END SELECT
END FUNCTION

Modify the statements above to create the following. See the DialogFunc help topic for more information.

FUNCTION DIALOGFUNC(DLGITEM$, ACTION%, SUPPVALUE&) AS BOOLEAN
SELECT CASE ACTION%
CASE 1 'DIALOG BOX INITIALIZATION
DLGTEXT "OK", "FIT"
CASE 2 'VALUE CHANGING OR BUTTON PRESSED
SELECT CASE DLGITEM$
CASE "OK" 'OK BUTTON PRESSED
NUMFITS = CINT(DLGTEXT("TEXTBOX1"))
END SELECT
END SELECT
END FUNCTION

The first statement (below the comment) initializes the dialog by changing the label in the OK button to “Fit”. Highlight DlgText and press F1 to learn more about this instruction. The second group of statements detects when the OK button has been pressed and then gets the value from the textbox and assigns it to the global variable NumFits. There is no error checking in this simple example so if the user leaves the textbox blank or enters a non-numeric value an error will occur.

There are three additional programming details that need to be addressed. We need to dimension the global variable NumFits so, at the top of the macro after Option Explicit, add the line

OPTION EXPLICIT DIM NUMFITS AS INTEGER

Next the Cancel button is a special case and must be dealt with separately from the DialogFunc function. The statement “Dialog dlg”, which appears after the End Dialog statement, activates the dialog and returns a zero if the Cancel button has been pushed. So change this line as shown below.

IF DIALOG(DLG) = 0 THEN GOTO FINISH DIM I AS INTEGER FOR I = 1 TO NUMFITS

and add the “Finish:” label at the end of the main subroutine

FINISH: END SUB

Pressing the Cancel button will now exit the dialog and jump to the Finish label thus terminating the macro.

Last, change the “2” in the for-loop to “NumFits” as shown above.

Save the macro and close the macro Edit window. Open Exponential Data Set 3.jnb that contains eight data sets. Double click on Graph Page 1 in the notebook to open the graph. Open the macro dialog from the Toolbox ribbon by clicking on the “Exponential Fit” button. Enter “8” in the dialog:

Click OK to run the macro and obtain the eight fit lines displayed with the data in the graph

The Exponential Data Set 3. jnb notebook also contains the completed macro named “Exponential Fit Example”.

If you run this macro, you will need to change the path in the macro to standard.jfl from:

Path = “C:\Users\[user]\Documents\SigmaPlot\SPW12\Standard.jfl”

to the path to your profile folder. With the graph window in focus, double click on this macro and click Run to run it.

Try SigmaPlot FREE for 30 Days!