Servicing Option Buttons, Options Buttons, List Boxes, etc.

The example program in Servicing Complex Dialog Boxes via Dialog Functions demonstrates how to service buttons in a custom dialog box using a dialog function (DialogFunc). The Visual Basic general syntax documentation describes how various other dialog box controls such as list boxes, etc., can be handled. Here is a fairly elaborate example program to illustrate how to service the various elements commonly used in dialog boxes.

' This program demonstrates how one can service various ' controls in a complex dialog. ' The following arrays will be used to handle the text ' in the list box and combo box. Dim ListArray(4) As String Dim ComboArray(5) As String ' In this program, the Main routine calls a subroutine ' that displays the dialog; note that you can "string together" ' many such subroutines to construct complex programs. Sub Main

If DisplayDialog = False Then

GoTo Finish

End If Exit Sub

Finish: End Sub ' This function brings up the dialog, and services the various ' controls in the dialog using the DialogFunc (dialog function) ' method. See also the general Statistica Visual Basic syntax ' help for additional details. Function DisplayDialog As Boolean

DisplayDialog=True

Begin Dialog UserDialog 390,336,"Demonstrating Dialog Controls", _ .MyDialogFunction ' %GRID:10,7,1,1

OKButton 300,14,70,21,.OkButton CancelButton 300,42,70,21,.CancelButton PushButton 30,14,160,21,"&Reset to defaults",.MyButton Text 30,49,90,14,"&Combo box:",.TextForMyComboBox ComboBox 130,49,160,70,ComboArray(),.MyComboBox Text 30,126,100,28,"&Text box (multiple lines):", _ .TextForTextBox TextBox 130,126,160,35,.MyTextBox,1 Text 30,175,90,14,"Value:",.TextForMyTextBox2 TextBox 130,168,160,21,.MyTextBox2 Text 30,196,60,14,"List box:",.TextForMyListBox DropListBox 130,196,160,91,ListArray(),.MyListBox CheckBox 30,231,130,14,"My Checkbox",.MyCheckBox GroupBox 30,252,180,63,"&Group Box",.MyGroupBox1 OptionGroup .MyOptionButtons OptionButton 40,273,150,14,"My Option Button &1" OptionButton 40,294,150,14,"My Option Button &2"

End Dialog

Dim dlg As UserDialog

' The following routine will initialize various controls in the ' dialog (fill the combo box, list box, check buttons, etc.)

InitializeUserDialogFields (dlg) TryAgain:

' The following line will display the dialog.

DisplayDialog=Dialog(dlg)

' If the user exited the dialog via OK, then DisplayDialog will ' be set to True; in that case, retrieve the chosen settings for ' the controls, and check for their correctness; if an error ' occurred, display the dialog again.

If DisplayDialog=True Then

If RetrieveUserDialogFields (dlg)=0 Then

MsgBox "Error in the dialog specs; try again.", vbCritical GoTo TryAgain

End If

End If

End Function ' 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. Private Function MyDialogFunction(DlgItem$, Action%, SuppValue&) As Boolean

Select Case Action%

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

' Set the default return code for MyDialogFunction to True; this will ' mean that the dialog will not close; only on OK and Cancel set this ' value to False, i.e., close the dialog.

MyDialogFunction = True

Select Case DlgItem

Case "MyButton"

' Reset all controls to their initial defaults

DlgValue "MyListBox", 0 DlgText "MyComboBox", ComboArray(1) DlgText "MyTextBox", _ "Initial text for multiple line edit field"

DlgText "MyTextBox2",".5" DlgValue "MyCheckBox", True DlgValue "MyOptionButtons", 1 MyDialogFunction=True

Case "MyListBox"

MsgBox "New combo box selection:" _ + ListArray(SuppValue)

Case "OkButton"

MyDialogFunction=False

Case "CancelButton"

MsgBox "The CANCEL button was pressed" MyDialogFunction=False

End Select

End Select

 End Function ' This subroutine initializes the list box, combo box, text ' fields, etc. ' Note that the entries in list boxes and combo boxes are ' zero-referenced, i.e., the first item in the list is ' referenced as element 0. Sub InitializeUserDialogFields (dlg)

ListArray(0)="List entry 0" ListArray(1)="List entry 1" ListArray(2)="List entry 2" ListArray(3)="List entry 3" ListArray(4)="List entry 4" dlg.MyListBox=0

ComboArray(0)="Combobox entry 0" ComboArray(1)="Combobox entry 1" ComboArray(2)="Combobox entry 2" ComboArray(3)="Combobox entry 3" ComboArray(4)="Combobox entry 4" ComboArray(5)="Combobox entry 5" dlg.MyComboBox=ComboArray(1) dlg.MyTextBox="Initial text for multiple line edit field" dlg.MyTextBox2=".5" dlg.MyCheckBox=True

 ' Note that the option buttons in a group of option buttons are ' zero-referenced, i.e., the first button in the group is referenced ' as element 0; thus here we set the second option button

dlg.MyOptionButtons=1

End Sub Function RetrieveUserDialogFields (dlg) As Boolean

On Error GoTo InvalidInput Dim xval As Double RetrieveUserDialogFields=True MsgBox "My combo box is set to: " + dlg.MyComboBox MsgBox "My multi-line text box is set to: " + dlg.MyTextBox xval=CDbl(dlg.MyTextBox2) MsgBox "My value was set to: " + Str(xval) MsgBox "My (0-referenced) element chosen in My Listbox is: " _ + Str(dlg.MyListBox)

MsgBox "My check box is set to: " + Str(dlg.MyCheckBox) MsgBox "My options buttons are set to (0-referenced): " _ + Str(dlg.MyOptionButtons)

Exit Function

InvalidInput:

RetrieveUserDialogFields=False

End Function

This example program is fairly complex and illustrates how to interact with the different standard Windows dialog box controls. Remember that the general SVB help contains detailed documentation for the statements, declarations, and data types used in this program.

Defining dialog boxes in subroutines; defining sequences of dialog boxes
In this example program, the dialog box is defined in and displayed by a subroutine (function), rather than the main program. Thus, by defining different dialog boxes in different subroutines, you can build elaborate programs with complex flow control, i.e., sequences of dialog boxes that depend on prior user choices or results of computations.

Zero-referencing in ListBox, DropListBox, and OptionGroup controls. Remember that the elements in ListBox, DropListBox, and OptionGroup controls are zero-referenced, i.e., the first element is referenced as number 0 (zero), the second as number 1, etc.

Retrieving numeric values
The program also illustrates how standard text controls can be used to return numeric values (see the .MyTextBox2 control). Specifically, the program retrieves the user-entered value as text, and later checks whether the text can be converted into a valid value of type Double; this is accomplished by defining an On Error Goto label in the RetrieveUserDialogFields function, where the program control will resume when an error occurs in the CDbl conversion function.
Changing controls from inside the dialog function
When you run the program and click on the Reset to defaults button (with the dialog ID "MyButton"), all fields will be reset to their defaults. This is done via the DlgText and DlgValue functions, as is illustrated in case "MyButton" in the dialog function.