Using Windows Common Dialog Boxes

From RAD Studio
Jump to: navigation, search

Go Up to Developing 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:

if OpenDialog1.Execute then
  filename := OpenDialog1.FileName;
if (OpenDialog1->Execute()) {
	filename = OpenDialog1->FileName;

The code shows the dialog box and indicates whether the user presses the OK button, and it copies the name of the file into a previously declared AnsiString variable named filename.

See Also