Write Windows Programs - Common Dialogs

 


1. What are the Windows Common Dialogs and how do I use them?

Microsoft have made available some predefined dialogs (windows) for common tasks. These can be used without needing further programming:

  • OpenFileDialog() - Displays dialog to open a file.
  • OpenFolderDialog() - Displays dialog to open a folder.
  • SaveAsDialog() - Displays dialog to save a file with a new name.
  • PrintDialog() - Displays dialog to print a file.
  • FontDialog() - Displays dialog to choose a font.
  • ColourDialog() - Displays dialog to choose pretty colours.

To use these dialogs just call the function. The dialog is then loaded, shown and unloaded automatically without further programming. Note that the open, save and print dialogs get the information needed to carry out the command but do not actually do the reading, saving or printing of files. Windows cannot carry out the actual tasks of saving, printing etc because it does not have the data to save/print, nor does it know how to save/print it.

2. What other Common Dialogs are available?

This table shows all the common dialogs available to your programs. The table also includes dialogs which are similar to the common dialogs, such as built-in modal windows like Msgbox, and other dialogs like the Aboutdialog which are in separate add-on classes:

Description Function name Details
About (program information) AboutDialog Display a modal dialog with some information about the program, and an optional logo. This dialog is in the Commdlgs class, and Commdlgs must be included in the uses clause to enable it.
Color selection ColorDialog / ColourDialog Display a modal dialog for selecting a color value - both UK English and American English spellings are allowed. This dialog is in the System class and is included automatically.
Find text FindDialog, FindNext Display a modal dialog with a field for entering a search string and options such as case-sensitive searching. FindNext is a companion dialog that repeats the last search. These dialogs are in the Commdlgs class, and Commdlgs must be included in the uses clause to enable them.
Font selection FontDialog Display a modal dialog for choosing a font. You can choose the font name, the point size, and the bold and italic state. This dialog is in the System class and is included automatically.
Input some text InputBox Display a modal dialog with a single entry field for typing in some text. This dialog is in the System class and is included automatically.
List of items ListBox Display a modal dialog with a list of items. One of the items can be selected and is returned by the dialog. This dialog is in the System class and is included automatically.
Message display MsgBox Display a modal dialog with some text and one or more buttons. When the dialog is closed, you can detect which button was pressed. This dialog is in the System class and is included automatically.
Open one or more files OpenFileDialog Display a modal dialog with a list of the files currently in a folder. You can navigate to different folders, and select one or more files using the dialog. This dialog is in the System class and is included automatically.
Open one or more folders OpenFolderDialog Display a modal dialog with a list of folders. You can navigate to different folders and different disk drives, and select a folder using the dialog. This dialog is in the System class and is included automatically.
Print (confirm details of print-out) PrintDialog Display a modal dialog with details of the selected printer and options to allow the printer and the number of copies to be changed. This dialog is in the System class and is included automatically.
Printer Setup (confirm printer to use) PrinterSetupDialog Display a modal dialog with a list of all connected printers, and return the selected printer. If you don't want the Printdialog to be able to change the printer, this provides an alternate way of doing the same thing. This dialog is in the Commdlgs class, and Commdlgs must be included in the uses clause to enable it.
Replace text ReplaceDialog Display a modal dialog with a field for entering a search string and a replacement string, and other options such as case-sensitive searching and global replacements. This dialog is in the Commdlgs class, and Commdlgs must be included in the uses clause to enable it.
Save file SaveAsDialog Display a modal dialog with a filename, and allow the filename and the location of the file to be changed. This dialog is in the System class and is included automatically.

3. How do I use the Windows MessageBox function?

Instead of MessageBox use MsgBox which is very similar, but allows extra buttons with customized text on them. There are several similar dialogs: MsgBox, InputBox and ListBox. These are shown using a single function call, and they don't need any extra setup work using the dialog editor or objects or properties.

4. How do I tell if the user pressed Cancel in a common dialog?

The common dialogs don't raise exceptions if the user pressed Cancel. Instead you can tell from the parameters returned by the dialog. If the dialog is closed or cancelled it returns empty strings instead of useful information.

5. How do I select more than one file using the OpenFileDialog?

Use the OPENDLG_MULTI flag, and the returned list of selected files (one or more) will be returned in a multi line string. Use the following code to get the returned files:

  files <- Openfiledialog("Open files", OPENDLG_MULTI, "*.*")
  for i from 1 to Strcount(files)
    file <- Strline(files,i)
    file <- StrRtrimCrLf(file)
    // use file here
  end for

The call to Openfiledialog allows one or more files to be selected. If the user chose any files and did not click the Cancel button, Strcount(files) will be greater than or equal to one. The for loop reads each line in the returned multi line string, removes the end of line characters from each line and copies it to the file variable. At this point the file variable contains each selected path name and is ready for use.

6. How do I choose a directory or a folder?

Use OpenFolderDialog. If the user presses OK when closing the dialog, the returned directory name is guaranteed to exist.