File/Directory selector

The File Selector dialog box is used to gather a file or directory name from the user.

It has the following characteristics:

  • The title bar can be set.

  • The file filters can be set.

  • The following error checking is provided:

    • Check to see if the file exists.

    • Check for proper permissions.

    • Check to see if the selection is a file.

  • Allows read-only access.

  • Accepts keywords and a target.

The file selection dialog box has the following prototypes:

AFXFileSelectorDialog(form, title, fileNameKw,
    readOnlyKw, opts, patterns, patternIndexTgt) 

AFXFileSelectorDialog(parent, title, fileNameKw,
    readOnlyKw, opts, patterns, patternIndexTgt)

You use the first constructor when you have a form associated with the dialog box that issues a command; for example, the dialog box that appears when you click FileOpen Database. You use the second constructor when the dialog box collects input from the user to be used in another dialog box. For example, when printing to a file from the Print dialog box, the user is presented with a text field to enter a file name and a Select button. The Select button posts a file selection dialog box that returns the selected file to the Print dialog box but does not issue any command.

You must create the fileNameKw argument using the AFXStringKeyword method. Similarly, you must create the readOnlyKw argument using the AFXBoolKeyword method. If the user clicks OK, the file selection dialog box automatically updates the fileNameKw and readOnlyKw arguments. In addition, when the dialog box is posted, it will set the current directory based on the path of the fileNameKw argument. This means that the dialog box remembers the last directory visited by the user when the application posts the dialog box again.

The following flags are available for the opts argument:

AFXSELECTFILE_EXISTING

Allows the selection of an existing file only.

AFXSELECTFILE_MULTIPLE

Allows the selection of multiple existing files only.

AFXSELECTFILE_DIRECTORY

Allows the selection of an existing directory only.

AFXSELECTFILE_REMOTE_HOST

Allows the opening of files on a remote host.

You specify the patterns argument as a series of patterns separated by \n. The value of the target specified by the patternIndexTgt argument determines which pattern is initially shown when the dialog box is posted.

The following is an example of how a file selection dialog box can be posted from a form:

def getFirstDialog(self):

    patterns = 'Output Database (*.odb)\nAll Files (*.*)'
    db = AFXFileSelectorDialog(self, 'Open ODB',
        self.nameKw, self.readOnlyKw, AFXSELECTFILE_EXISTING,
        patterns, self.patternIndexTgt)
    db.setReadOnlyPatterns('*.odb')
    self.setModal(True)
    return db 

The following is an example of how a directory selection dialog box can be posted from another dialog box:

def onCmdDirectory(self, sender, sel, ptr):

    if not self.dirDb:
        self.dirDb = AFXFileSelectorDialog(self,
            'Select a Directory',self.form.dirNameKw,
             None, AFXSELECTFILE_DIRECTORY)
        self.dirDb.create()

    self.dirDb.showModal()
    return 1