RAD Studio |  |

Using Windows Common Dialog Boxes

One of the commonly used dialog box components is TOpenDialog. This component is usually invoked by a New or Open menu item under the File option on the main menu bar of a form. The dialog box contains controls that let you select groups of files using a wildcard character and navigate through directories.

The TOpenDialog component makes an Open dialog box available to your application. The purpose of this dialog box is to let a user specify a file to open. You use the Execute method to display the dialog box.

When the user chooses OK in the dialog box, the user's file is stored in the TOpenDialog FileName property, which you can then process as you want.

The following code can be placed in an Action and linked to the Action property of a TMainMenu subitem or be placed in the subitem's OnClick event:

Copy Code

    if OpenDialog1.Execute then
       filename := OpenDialog1.FileName;

Copy Code

    if(OpenDialog1->Execute()){
       filename = OpenDialog1->FileName;
    };

This code will show the dialog box and if the user presses the OK button, it will copy the name of the file into a previously declared AnsiString variable named filename.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!