FMXTOpenDialog (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to use the TOpenDialog filters to show only certain files in the dialog.

To build and test this example, create a Multi-Device Application - Delphi, and add the following on the form:

  • A TOpenDialog object to display the dialog that chooses what file(s) to open.
  • A TComboBox object used for selecting which types of files are shown in the newly opened dialog.
  • A TButton to trigger the Execute property of the TOpenDialog object.
  • Three TListBoxItems that filter the TOpenDialog results. These can be added manually from the Items Designer menu, accessible by double-clicking the TComboBox component.
  • A TLabel that displays the following text: "Select File Type:".

Code

Before writing the code, include the FMX.Media unit scope in the uses clause. Also, rename the three TListBoxItems to Text, Multimedia and Image.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Text.IsSelected then
  begin
    // Shows the text files only.
    OpenDialog1.Filter := 'Text Files (*.txt)|*.txt';
    OpenDialog1.Execute;
  end
  else if Image.IsSelected then
  begin
    // Shows image files only.
    OpenDialog1.Filter := FMX.Types.TBitmapCodecManager.GetFilterString;
    OpenDialog1.Execute;
  end
  else if Multimedia.IsSelected then
  begin
    // Shows audio/video files only.
    OpenDialog1.Filter := FMX.Media.TMediaCodecManager.GetFilterString;
    OpenDialog1.Execute;
  end
  else
    // Select a type of file from the TComboBox.
    ShowMessage('Select a type of file');
end;

Uses