9.1. Sample Script for a Dynamic Dialog Box

The following script illustrates the most important concepts you'll need to understand in order to create a dynamic dialog box in your script:

'Dim "Fruits" and "Vegetables" arrays here to make them accessible to
'all procedures.
Dim Fruits(2) As String
Dim Vegetables(2) As String

'Dialog procedure--must precede the procedure that defines the custom
'dialog box.
Function DialogControl(ctrl$, action%, suppvalue%) As Integer
 Select Case action%
  Case 1
   DlgListBoxArray "ListBox1", fruits 'Fill list box with
                'items before dialog
                'box is visible.
   DlgValue "ListBox1", 0   'Set default value to
               'first item in list box.

  Case 2
   'Fill the list box with names of fruits or vegetables
   'when the user selects an option button.
   If ctrl$ = "OptionButton1" Then
     DlgListBoxArray "ListBox1", fruits
     DlgValue "ListBox1", 0

   ElseIf ctrl$ ="OptionButton2" Then
     DlgListBoxArray "ListBox1", vegetables
     DlgValue "ListBox1", 0
  End If
 nd Select
End Function

Sub Main()
 Dim ListBox1$()  'Initialize array for use by ListBox
        'statement in template.
 Dim Produce$

'Assign values to elements in the "Fruits" and "Vegetables"
'arrays.
 Fruits(0) = "Apples"
 Fruits(1) = "Oranges"
 Fruits(2) = "Pears"
 Vegetables(0) = "Carrots"
 Vegetables(1) = "Peas"
 Vegetables(2) = "Lettuce"

 'Define the dialog box template.
 Begin Dialog UserDialog ,,163,94,"Grocery Order",.DialogControl
 Text 13,6,32,8,"&Quantity:",.Text1 'First control in
             'template gets the focus.
  TextBox 48,4,28,12,.TextBox1
  ListBox 12,28,68,32,ListBox1$,.ListBox1
  OptionGroup .OptionGroup1
   OptionButton 12,68,48,8,"&Fruit",.OptionButton1
   OptionButton 12,80,48,8,"&Vegetables",.OptionButton2
  OKButton 112,8,40,14
  CancelButton 112,28,40,14
 End Dialog

 im b As UserDialog   'Create the dialog record.
 b.TextBox1 = "12"   'Set the default value of the text
           'box to 1 dozen.
 response% = Dialog(b)   'Display the dialog box.

 Select Case response%
  Case -1
   If b.OptionGroup1 = 0 Then
     produce$ = fruits(b.ListBox1)
   Else
     produce$ = vegetables(b.ListBox1)
   End If

   MsgBox "Thank you for ordering " & b.TextBox1 & " " & produce$ & "."

  ase 0
   MsgBox "Your order has been canceled."
 End Select
End Sub

More information

9. Use a dynamic dialog box in a script.