The Open File Dialog Box



One of the most usual involvements with computer files consists of opening them for review or for any other reason the user judges appropriate. Microsoft Windows provides a convenient dialog box to handle the opening of files. The job is performed by using the Open File dialog box:


Open File Dialog Box Creation

To provide the means of opening files, you can use the TOpenDialog class. The easiest way to use it is to click the OpenDialog button from the Dialogs tab of the Tool Palette and click on the form. The OpenDialog icon can be positioned anywhere on the form because it would not be seen at run time. After placing it on the form, you can use the Object Inspector to configure it.

If you prefer to dynamically create an Open dialog box, declare a pointer to TOpenDialog and use the new operator to call its constructor and specify its owner. The technique is the same we applied for the TSaveDialog class. Here is an example:

TOpenDialog *OpenMe = new TOpenDialog(Form1);

Characteristics of an Open Dialog Box

One of the most important properties of an Open dialog box is the file it presents to the user. This is represented by the FileName property. If you want a default file to be specified when the dialog box comes up, you can specify this in the FileName property of the Object Inspector. If you need to use this property, you should make sure the file can be found. If the file is located in the same folder as the application, you can provide just its name. If the file is located somewhere in the hard drive, you should provide its complete path. Most of the time, you will not be concerned with this property if you are creating an application that will allow the user to open any file of her choice. Once a file is located, it can be accessed using the TOpenDialog::FileName property.

To make your application more effective, you should know what types of files your application can open. This is taken care by specifying a list of extensions for the application. To control the types of files that your application can open, specify their extensions using the Filter Property. The Filter string is created exactly like that of a SaveDialog control as we saw earlier.

Like the SaveDialog control, the default extension is the one the dialog box would first filter during file opening. If you want the Open File dialog to easily recognize a default type of file when the dialog box opens, you can specify the extension's type using the DefaultExt property.

For convenience, or for security reasons, Open File dialog boxes of applications are sometimes asked to first look for files in a specific location when the Open File dialog box comes up. This default folder is specified using the InitialDir property.

The essence of using the Open File dialog box is to be able to open a file. This job is handled by the Execute() method which is easily called using a pointer to the OpenDialog object you are using.

Practical LearningPractical Learning: Using the Open Dialog Box

  1. In the Dialogs section of the Tool Palette, click the TOpenDialog button Open File Dialog and click the form
  2. While the OpenDialog1 icon is still selected on the form, in the Object Inspector, click DefaultExt and type rtf
  3. Click Filter and click its ellipsis button
  4. Complete the Filter Editor dialog box as follows:
    Filter Editor
  5. Click OK
  6. Click Title, type Open an Existing Document and press Enter
  7. Click an unoccupied area on the form to select it and, in the Object Inspector, click the Events property page
  8. Double-click the OnMouseDown field and implement the event as follows:
    void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
    				      TShiftState Shift, int X, int Y)
        if( Button == mbRight )
    	if( OpenDialog1->Execute() == True )
    	   ShowMessage(L"The Open button was clicked or the Enter key was pressed"
    		       L"\nThe " + OpenDialog1->FileName
    		       L" file would have been opened.");
    	    ShowMessage(L"The Cancel button was clicked or Esc was pressed");
  9. Press F9 to test the application
  10. Right-click the form
  11. Locate and select any file
  12. Click Open:
  13. After using it, close the form and return to your programming environment

Home Copyright © 2010-2011 FunctionX, Inc.