Servicing Complex Dialog Boxes via Dialog Functions (DialogFunc)

The simple method of servicing the dialog box illustrated in the A Simple Dialog Box in Statistica Visual Basic example has the disadvantage that the dialog box closes after you click any button. When designing complex user interfaces, you often want the user to set various options in a dialog box, and only exit the dialog box via the OK or Cancel button. To accomplish this, specify a DialogFunc (dialog function) handler subroutine (see also the general SVB help for more information on the syntax of dialog functions). Here is the example shown in the previous example, rewritten to use this method of servicing the dialog box.

' This program brings up a simple dialog box, and services
' the buttons in the dialog using the DialogFunc (dialog

' function) method.

Sub
Main

Dim ReturnId As Integer

' Note that we added MyDialogFunction as the name of the
' dialog procedure (function) that will service this dialog

Begin Dialog UserDialog 400,203, _
.MyDialogFunction
' %GRID:10,7,1,1

' The OkButton was given the explicit name "OkButton"

OKButton 300,14,70,21,.OkButton

' The CancelButton was given the explicit name "CancelButton"

CancelButton 300,42,70,21,.CancelButton

' The "My Button!" was given the explicit name "MyButton"

PushButton 30,14,90,21,"&My Button!",.MyButton

End Dialog

Dim dlg As UserDialog
ReturnId=
Dialog(dlg)

End Sub

' This is the (private) dialog function that services the UserDialog.

' The name of this function must match the name given to the Dialog

' (see the .MyDialogFunction above).

' Three parameters are passed to the dialog function. Refer to the

' general syntax help for additional details about each parameter.

' Note that the dialog will terminate (close) when the MyDialogFunction

' returns False; when it returns True, the dialog will remain on the

' screen.

Private Function
MyDialogFunction(DlgItem$, Action%, SuppValue&) As Boolean

Select Case Action%
Case
1 ' Dialog box initialization
Case
2 ' Value changing or button pressed

MyDialogFunction = True

Select Case
DlgItem

Case
"MyButton"

MsgBox "The MY BUTTON was pressed"
MyDialogFunction=
True

Case "OkButton"

MsgBox "The OK button was pressed"
MyDialogFunction=
True

Case "CancelButton"

MsgBox "The CANCEL button was pressed"
MyDialogFunction=
False

End Select

End Select

End Function

When you run this program, you will notice that now you can click the OK and My Button buttons, without closing the dialog box. When you click the Cancel button, the dialog box closes. As noted in the comments of the program, when the MyDialogFunction returns False, the dialog box closes; when it returns True, it remains on the screen.