The dialog function

<< Click to Display Table of Contents >>

The dialog function

You can optionally connect a user-defined dialog box to a dialog function. This function is invoked whenever the dialog field is initialized or the user interacts with a dialog control. With the help of a dialog function, it is possible to nest dialogs and to enable or disable dialog controls.

To connect a dialog box to a dialog function, append the function's name to the dialog definition, with a period in front of it. Here, for example, the dialog MyDlg will be connected to the dialog function with the name MyDlgFunc:

Begin Dialog MyDlg 60, 60, 260, 188, "Test", .MyDlgFunc

Monitoring dialog controls

Every control in the dialog box that you wish to monitor in the dialog function must have a unique identifier. It must be given as the last parameter of the control definition and must start with a period.

CheckBox 8, 56, 203, 16, "Show all", .Chk1

Here, the identifier "Chk1" is assigned to the check box.

Syntax of the dialog function

The syntax of the dialog function is as follows:

Function FunctionName(ControlID$, Action%, SuppValue%)

 [Statements]

 FunctionName = ReturnValue

End Function

The dialog function returns a value if the user clicks on OK or Cancel. If you set this ReturnValue in the dialog function to 0, the dialog will close; with any other value, the dialog stays open.

The parameters of the dialog function:

ControlID$

If Action = 2, this parameter contains the ID of the dialog control that the user activated (the value of the ID was defined in the dialog definition).

Action%

1 when the dialog is initialized (in this case, the other parameters have no meaning).
2 when the user activates a dialog control. The dialog control is identified through ControlID$, and SuppValue% contains additional information.

SuppValue%:

Information on the type of change that was made, depending on the type of the dialog control:
Check box: If the box is unchecked, this is 0, else 1.
Radio button: The number of the selected radio button, with the first field of the radio button group having the number 0.
Command button: No meaning
OK: 1
Cancel: 2

In the following example, the dialog function of a dialog is evaluated by means of a Case branch. The parameter SuppValue is not tested in this example.

Sub Main

 

 Begin Dialog UserDialog1 60,60, 260, 188, "Dialog Function", .Dialogfn

         Text 8, 10, 73, 13, "Text:"

         TextBox 8, 26, 160, 18, .FText

         CheckBox 8, 56, 203, 16, "Show all",. Chk1

         GroupBox 8, 79, 230, 70, "Group box:", .Group

         CheckBox 18, 100, 189, 16, "Change the button caption", .Chk2

         PushButton 18, 118, 159, 16, "Button", .History

         OKButton 177, 8, 58, 21

         CancelButton 177, 32, 58, 21

 End Dialog

 Dim Dlg1 As UserDialog1

 x = Dialog(Dlg1)

End Sub ' (Main)

Function Dialogfn(ControlID$, Action%, SuppValue%)

 Begin Dialog UserDialog2 160,160, 260, 188, "Dialog Function", .Dialogfunction

         Text 8,10,73,13, "Input Field"

         TextBox 8, 26, 160, 18, .FText

         CheckBox 8, 56, 203, 16, "Check box ",. ch1

         CheckBox 18, 100, 189, 16, "Check box", .ch2

         PushButton 18, 118, 159, 16, "Button", .but1

         OKButton 177, 8, 58, 21

         CancelButton 177, 32, 58, 21

 End Dialog

 Dim Dlg2 As UserDialog2

 Dlg2.FText = "This is the result"

 Select Case Action%

         Case 1

                 DlgEnable "Group", 0

                 DlgVisible "Chk2", 0

                 DlgVisible "History", 0

         Case 2

                 If ControlID$ = "Chk1" Then

                         DlgEnable "Group"

                         DlgVisible "Chk2"

                         DlgVisible "History"

                 End If

                 If ControlID$ = "Chk2" Then

                         DlgText "History", "Show another dialog"

                 End If

                 If ControlID$ = "History" Then

                         Dialogfn = 1

                         x = Dialog(Dlg2)

                 End If

         Case Else

 End Select

 Dialogfn = 1

End Function