Single Selection Controls in VBScript

This page will describe how to use radio buttons, select lists, and select menus (also called drop-down lists) in your ScriptDialog in VBScript. These three sets of controls all allow users to select a single item from a list or group of items.

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

 Listing 6.1, tut6.vbs 
Sub main
  ' Create the dialog box
  Set Dialog = CreateObject("TPFSoftware.ScriptDialog")
 ' Size the dialog box
  Dialog.SetBounds "center", "center", 231, 249
  ' Set the dialog box title
  Dialog.Title = "Three Bears Inn"
 ' Add a group of radio buttons
  Dialog.AddRadios "Porridge", "Porridge:", _
    Array("too hot", "too cold", "just right"), "too cold"
  ' Add a select list
  Dialog.AddSelectList "Chair", "Rocking Chair:", _
    Array("too fast", "too slow", "just right"), _
    "too slow", 3
  ' Add a select menu
  Dialog.AddSelectMenu "Bed", "Bed:", _
    Array("too hard", "too soft", "just right"), "just right"
  ' 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
    PorridgeQuality = Result.ValueOf( "Porridge" )
    ChairQuality = Result.ValueOf( "Chair" )
    BedQuality = Result.ValueOf( "Bed" ) 


    MsgBox "The porridge was " & PorridgeQuality & _           " and the rocking chair was " & ChairQuality & _           " and the bed was " & BedQuality   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 6 below. Clicking a button will display a message telling you what you did.

Figure 6: Dialog box produced by tut6.vbs.

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

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

To add a drop-down list of items to a dialog box, use the AddSelectMenu method. AddSelectMenu takes four arguments that are identical in function to those of AddRadios.

To find out what radio button or list item the user selected, use the ValueOf method which belongs to the Result object. ValueOf takes the name of the radio button group or list control as an argument and returns the caption of the selected radio button or list item.