Using Dialog Boxes from Windows

In order to save developers who are writing applications for Windows from having to create certain dialog boxes from scratch, Windows provides a set of common dialog boxes. Among them are the File dialog box, which you use to open, save, or browse for files on your computer or computer network; the Browse For Folder dialog box, which you use to specify a folder name; and the Color dialog box, which allows you to choose a specific color.

In the wizard you're creating, you'll take advantage of the File and Browse For Folder dialog boxes. The code provided in the practice files allows you to use these dialog boxes without knowing too much about the code itself, which uses more advanced Windows-based programming techniques. You can learn more about the specifics of this code and Windows programming using the Visual Basic programming language by referring to books on the Visual Basic and Windows application programming interfaces (APIs).

Add a Common Windows Dialog Box to Your Wizard

  1. In the Project Explorer, right-click the Energy Wizard project and click Import File on the shortcut menu. In the Import File dialog box, change to the Chapter 3 practice folder and select the file modFlDlg.bas. Click Open.
  2. The file modFlDlg.bas contains code that allows you to display the Windows system File dialog box, which is the same dialog box as the Import File dialog box you just used. The name of the imported standard module is modFileDialog, and it also contains a user-defined type that you can use to set properties such as the caption in the title bar of the File dialog box.

  3. Click within the MultiPage control and select pgStep1 Page from the drop-down list in the Properties window. Double-click the first Browse button and add the following code to the cmdBrowse_Click event procedure, so that the procedure appears as follows:
  4. Private Sub cmdBrowse_Click()
        Dim sFileName As String
        Dim udtFileDialog As FileDialog   
        With udtFileDialog
            .CustomFilter = "Access Database (*.mdb)" & - 
                Chr$(0) & "*.mdb" & Chr$(0) & Chr$(0)
            .DefaultExt = "*.mdb"
            .Title = "Browse"
            sFileName = modFileDialog - 
                .WinFileDialog(udtFileDialog, 1)
        End With
        If Len(sFileName) > 0 Then
            txtDataSource.Text = sFileName
        End If
    End Sub
    

    The variable udtFileDialog is declared as the user-defined type FileDialog, which is declared as public in the modFileDialog module. You use the user-defined type to set the filters that are displayed in the Files Of Type drop-down list in the File dialog box.

    The default extension is then set to the filter extension that the Files Of Type drop-down list will display first. When you click the cmdBrowse control, you use the Access Database type filter. The title bar caption of the File dialog box is set to the text "Browse."

    When you display the dialog box, the folder you'll see by default is the most recently used folder. The section File Management in Chapter 4 describes how to set which folder is initially displayed in this dialog.

    The value of sFileName is then set to the value returned by the function WinFileDialog, which displays the File dialog box. The If…Then condition block determines whether you clicked Cancel in the File dialog box or selected a valid file and clicked Open. If you selected a valid file, the program enters its full path in the txtDataSource text box. If you clicked Cancel, the value of sFileName is an empty string.

  5. In the Object drop-down list of the module, select cmdBrowseTemplate and copy the same code to the cmdBrowseTemplate_Click event procedure that you added in the previous step to the cmdBrowse_Click event procedure. Change the two lines setting the value of CustomFilter and DefaultExt to the following:
  6. .CustomFilter = "Document Template (*.dot)" & - 
        Chr$(0) & "*.dot" & Chr$(0) & Chr$(0)
    .DefaultExt = "*.dot"
    

    When you click the cmdBrowseTemplate control, the filters used are of the Document Template type.

  7. Change the line txtDataSource.Text = sFileName in the cmdBrowseTemplate_Click event procedure to:
  8. txtTemplate.Text = sFileName
    

    If a valid file was selected, its full path is entered in the txtDataSource text box.

  9. Select pgStep2 Page from the drop-down list in the Properties window and then select cmdBrowseFolder from the drop-down list in the Properties window. Add the following code to the cmdBrowseFolder_Click event procedure:
  10. Private Sub cmdBrowseFolder_Click()
        Dim sPath As String    
        sPath = modBrowseFolder.BrowseForFolder
        If Len(sPath) > 0 Then
            txtDestination.Text = sPath
        End If
    End Sub
    

    The function BrowseForFolder in the modBrowseFolder module displays the Windows system Browse For Folder dialog box shown below. (Your Browse For Folder dialog box will look different.)

  11. In the Project Explorer, right-click the Energy Wizard project and click Import File on the shortcut menu. In the Import File dialog box, change to the Chapter 3 practice folder and select the file modBrwse.bas. Click Open.
  12. The file modBrwse.bas contains code that allows you to display the Windows system Browse For Folder dialog box. The name of the imported standard module is modBrowseFolder.

  13. Click the frmWizard project item and then click the View Object button in the Project Explorer to display the wizard UserForm.

  14. View Object

  15. Press F5 to run the wizard. Step through the wizard and click each of the Browse buttons in the steps. The wizard now allows you to find the files.
  16. Close and unload the frmWizard UserForm by clicking the Cancel or Finish button.
  17. Right-click the frmWizard project item in the Project Explorer and then click Export File on the shortcut menu. In the Export File dialog box, change to the Chapter 3 practice folder, and in the File Name text box, type the name frmWizardComplete and click Save.