At the beginning of this Daily Drill Down series, I showed you how to create a reusable DialogBox function. Next, I explained how to create text boxes, radio buttons, and check boxes to enhance the look and feel of your dialog boxes. Now I turn your attention to creating drop-down list boxes, analyzing the results returned from your dialog boxes, and pulling the whole thing together into one neat user-friendly package. I have also collected the information from the series and included the final product in a download.
Working with drop-down list boxes
Drop-down list boxes provide you with a neat way to prompt users to select from a list of possible items. A drop-down list box, often called a combo box, looks like a text box with an arrow at the end of the box. When the user clicks the arrow, a list of choices drops down and the user can select one of the items in the list.
Creating the drop-down list box control in HTML
To create a check box control, use the HTML <Select> tag in this format:
<Select Name=“name” Size=”#”>
The Name attribute is assigned a name that you’ll later use to reference the drop-down list box control from within VBScript. The Size attribute simply specifies how many of the options in the drop-down list box are visible in the dialog box. Between each of the Option tags, you’ll specify an item that you want to appear in the list box.
For example, if you want to create a dialog box that will prompt a user to select a department from a list, you can configure the drop-down list box control using these lines:
<p>Please select your department:<br>
<select name="Department" size="1">
If you were to insert these lines in the HTML dialog box template, the dialog box generated by the DialogBox function would look like the one shown in Figure A.
|Inserting the drop-down list box control commands shown above into the HTML dialog box template and then sending it to the DialogBox function would generate this dialog box.|
Analyzing the drop-down list box control in VBScript
Handling the results from a drop-down list box in VBScript is similar to handling radio buttons and check boxes because you use the Elements object. However, instead of being stored in an array, items from a drop-down list box are stored in a special type of array called a collection. This means that additional steps are required to obtain the results from a drop-down list box. Let’s take a closer look:
Set DropDownDialog = IEDbx.Document.Dbx.Elements
Set DropDownItem = DropDownDialog("Department")
For Each Item in DropDownItem.Options
If Item.Selected Then
Depart = Item.Text
As you can see, the first thing I do is establish an object variable called DropDownDialog that can now be used to reference the Elements object. I then set up another object variable called DropDownItem to access the items in the collection. This collection is referred to the name assigned by the drop-down list box in the HTML code—in this case, Department.
Now, when I set up the For Each…Next looping statement, I can access the collection using the DropDownItem object variable and the Options property. I then use an If…Then statement to test the Selected property, which will contain the Boolean value True if that particular item was selected from the drop-down list box. If so, I then assign the value stored in the Text property, which is the name of the selected item, to the Depart variable. At this point, I use the MsgBox function to display the contents of the Depart variable, which now contains the name of the department that was selected in the drop-down list box.
Putting it all together
Now that you have a good idea of how dialog box controls are created in HTML and handled in VBScript, let’s put them all together. Listing A shows the HTML code for a dialog box that contains all the controls we have covered in this Daily Drill Down series. To create this file, I started with the HTML Dialog Box template that I presented in a previous article and then added the code for each of the dialog box controls.
Listing A: AllTogether.htm
- <body bgcolor="#D3D1CF" scroll="no">
- <script language="VBScript">
- Dim Continue
- Sub Window_OnLoad()
- Continue = 0
- End Sub
- Sub button1_OnClick
- Continue = 1
- End Sub
- Sub button2_OnClick
- Continue = 3
- End Sub
- Public Function ClickButton
- ClickButton = Continue
- End Function
- <form name="Dbx">
- <p>Please type your first and last name:<br>
- First name: <input type="text" size="20" name="fname"> <br>
- Last name: <input type="text" size="20" name="lname"></p>
- <p>Please select your department<br>
- <select name="Department" size="1">
- <option>Customer Relations</option>
- <p>Please select you computer </p>
- <div align="left"><table border="0" width="350">
- <tr><td width="50%">
- <input type="radio" name="Comp" value="Compaq Deskpro EN">Compaq Deskpro EN<br>
- <input type="radio" name="Comp" value="Dell Dimension L">Dell Dimension L<br>
- <input type="radio" name="Comp" value="Gateway Profile">Gateway Profile
- </td><td width="50%">
- <input type="radio" name="Comp" value="Compaq Deskpro EXD">Compaq Deskpro EXD<br>
- <input type="radio" name="Comp" value="Dell Dimension 4100">Dell Dimension 4100<br>
- <input type="radio" name="Comp" value="Gateway Profile">Gateway Performance
- <p>Please select the applications installed on your computer </p>
- <div align="left"><table border="0" width="200">
- <tr><td width="50%">
- <input type="checkbox" name="App" value="Word">Word<br>
- <input type="checkbox" name="App" value="Excel">Excel<br>
- <input type="checkbox" name="App" value="Access">Access</td>
- <td width="50%">
- <input type="checkbox" name="App" value="PowerPoint">PowerPoint<br>
- <input type="checkbox" name="App" value="Outlook">Outlook<br>
- <input type="checkbox" name="App" value="FrontPage">FrontPage</td>
- <p><input type="button" name="button1" value=" OK ">
- <input type="button" name="button2" value=" Cancel "></p>
The only difference between this version and the individual sections I presented earlier is that I’ve placed the radio button and check box controls in tables in order to better format the dialog box. Each table has one row and two columns. I then split the controls between the two rows.
In line 37 I specified that the table for the radio buttons is 350 pixels wide. Then in lines 38 and 42, I specified that each column width is 50 percent. In line 48, I specified that the table for the check boxes is 200 pixels wide. Lines 49 and 53 set the column width to be 50 percent. Setting the table width in pixels and the column width in a percentage makes it easy to adjust the size of the table so that the controls fit nicely.
When the AllTogether.htm file is sent to the DialogBox function, this HTML code will generate the dialog box shown in Figure B.
|The AllTogether.htm file will create this dialog box.|
Creating dialog box controls the visual way
While you can create dialog box controls simply by typing the appropriate HTML form commands in a text editor such as Notepad, you’ll have much better success if you use a visual HTML editor. You can then load the HTML dialog template into the editor and visually create your dialog box controls. There are literally hundreds of shareware HTML editors that you can download from the Internet. If you have Microsoft Office, you can even use FrontPage. However, for the simple job of creating dialog boxes for use in VBScript, I prefer to use Microsoft FrontPage Express, the little HTML editor that made its first appearance in Windows 98 and Internet Explorer 4.0. While Microsoft doesn’t officially support FrontPage Express anymore, you can still download a copy of it from Microsoft’s Internet Explorer 5.01 Service Pack 1 site. Once you download the FrontPage CAB file, simply open it in WinZIP. Don’t worry about installing it—just create a folder on your hard disk and copy all the files from the CAB file to that folder. You can then create a shortcut to FrontPage Express’ executable file—Fpxpress.exe—and place it on your desktop or Start menu. FrontPage Express runs fine in Windows 9x, Windows NT, and Windows 2000.
Dialog box driver script analyzed
The dialog box driver script, AllTogether.wsf, is shown in Listing B. To create this file, I started with the Windows Script File Driver template that I presented in the previous article and then added the VBScript for analyzing the dialog box controls.
Listing B: AllTogether.wsf
- <?XML Version="1.0" ?>
- <?job error="true" debug="true"?>
- <job id="VB Code">
- <script language="VBScript" src="FDialogBox.vbs">
- Dim IEDbx, FileSystem, DbxFile, Cancel
- Set WshShell = WScript.CreateObject("WScript.Shell")
- Set FileSystem = CreateObject("Scripting.FileSystemObject")
- Set IEDbx = WScript.CreateObject("InternetExplorer.Application")
- Cancel = False
- DbxFile = "AllTogether.htm"
- Call DialogBox(100, 60, 486, 383, Cancel, DbxFile)
- If Cancel Then
- Set IEDbx = Nothing
- End If
- FirstName = IEDbx.Document.Dbx.fname.Value
- LastName = IEDbx.Document.Dbx.lname.Value
- Set DropDownDialog = IEDbx.Document.Dbx.Elements
- Set DropDownItem = DropDownDialog ("Department")
- For Each Item in DropDownItem.Options
- If Item.Selected Then
- Depart = Item.Text
- Exit For
- End If
- Set DropDownDialog = Nothing
- Set DropDownItem = Nothing
- Set RadioButtonDialog = IEDbx.Document.Dbx.Elements("Comp")
- For Each RadioButton in RadioButtonDialog
- If RadioButton.Checked = True Then
- Comp = RadioButton.Value
- Exit For
- End If
- Set RadioButtonDialog = Nothing
- Set CheckBoxDialog = IEDbx.Document.Dbx.Elements("App")
- List = ""
- For Each CheckBox in CheckBoxDialog
- If CheckBox.Checked = True Then
- List = List & vbCrLf & CheckBox.Value
- End IF
- Set CheckBoxDialog = Nothing
- Results = "You chose the following:" & vbCrLf &_
- FirstName & " " & LastName & vbCrLf &_
- Depart & vbCrLf &_
- Comp & vbCrLf &_
- MsgBox Results
- Set IEDbx = Nothing
The VBScript code used to analyze the results from each of the dialog box controls is basically the same as the individual sections I showed you earlier. There are a few differences, due to the fact that all the code appears in one file. In lines 28 and 29, I reset the DropDownDialog and Set DropDownItem object variables by using the Set command to assign their contents to the VBScript Nothing keyword. This is basically a clean-up step that releases any memory and system resources associated with the object variables. As you’ll notice, I repeated this operation in lines 37 and 45 for the other interim object variables.
You’ll also notice that in lines 30 and 38 I added the names of the dialog box controls to the Elements object. This focuses the Elements object on a specific control and is necessary in this dialog box because it contains multiple controls.
In lines 46 through 50, I compile the results from the various dialog box controls and assign them to a variable called Results. I then use the MsgBox function to display those results, as shown in Figure C.
|The AllTogether.wsf file will display the results in a simple message box.|
The reusable DialogBox function is a handy feature I use often in my daily coding life. It's flexible, easy to master, and comes with a whole host of features you can use to manipulate the options presented to the user. In this Daily Drill Down series, I’ve shown you how to populate your HTML dialog box files with text boxes, radio buttons, check boxes, and drop-down list boxes. I’ve also shown you how to work with the results in VBScript. By now, you should have an excellent handle on creating your own dialog boxes using this Daily Drill Down series as your guide.
Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.