Multi-Select Controls in VBScript

This page will describe how to use checkboxes and multi-select lists in your ScriptDialog in VBScript. These two sets of controls all allow users to select multiple items from a group of items.

Create a new VBScript script. Then insert the following code and save it as tut7.vbs.

 Listing 7.1, tut7.vbs 
Sub main
  ' Create the dialog box
  Set Dialog = CreateObject("TPFSoftware.ScriptDialog")
  ' Size the dialog box
  Dialog.SetBounds "center", "center", 231, 240
  ' Set the dialog box title
  Dialog.Title = "Three Bears Inn"
  ' Add a group of checkboxes
  Dialog.AddCheckboxes "Activities", "Activities:", _
    Array("eating", "sitting", "sleeping"), _
    Array("eating", "sitting")
  ' Add a multi-select list
  Dialog.AddMultiSelectList "Nearby", "Nearby Attractions:", _
    Array("straw house", "stick house", "brick house"), _
    Array("brick house"), 3
  ' Add a row of buttons
  Dialog.AddButtons "button", Array("OK", "Cancel")
 ' Display the dialog box
  Set Result = Dialog.Execute
 ' Find out which button was clicked
  If Result.ValueOf("button") = "OK" Then
    Activities = Result.ArrayOf("Activities")
    Nearby = Result.ArrayOf("Nearby")
    MsgBox "You engaged in the following:" & VBCRLF & _
           Join(Activities, ", ") & VBCRLF & _
           "You visited the following:" & VBCRLF & _
           Join(Nearby, ", ")
  Else
    MsgBox "You did not press OK"
  End If
End Sub

When you run the script, you'll see a dialog box that looks like Figure 7 below. To select multiple items in the select list, hold down the Ctrl key when clicking with the mouse. Clicking the OK or Cancel push button will display a message telling you what you did.

Figure 7: Dialog box produced by tut7.vbs.

Analysis
To add a group of checkboxes to a dialog box, use the AddCheckboxes method. AddCheckboxes takes four arguments, the last optional. The first is the name of the group of checkboxes you are adding. The second is the label that will appear beside the group of checkboxes. The third is an array of captions, one for each checkbox in the group. The fourth (and optional argument) is an array of captions for the checkboxes that are selected by default.

To add a multi-select list of items to a dialog box, use the AddMultiSelectList method. AddMultiSelectList takes five arguments. The first four arguments are identical in function to those of AddCheckboxes. The fifth (and optional) argument is the height in lines that the select list will occupy on the dialog box.

To find out what checkboxes or list items the user selected, use the ArrayOf method which belongs to the Result object. ArrayOf takes the name of the checkbox group or list control as an argument and returns an array that contains the captions of the selected checkboxes or list items.