Search code examples
delphifilteropenfiledialog

TFileOpenDialog FileTypes : how to change at runtime


In a TFileOpenDialog I set the FileTypes property to a custom list of files populated by selected files from the current folder.

dlg := TFileOpenDialog.Create( self );
Filter := TStringList.Create();
// ... 
// Filter contains the selected files
  if Filter.Count>0 then
    begin
      msg := '';
      for i := 0 to Filter.Count-1 do
          msg := msg + Filter[i] + ';';
      dlg.FileTypes.Clear;
      dlg.FileTypes.Add.DisplayName := '';
      dlg.FileTypes.Add.FileMask := msg;
    end;

It works as desired.

I handle the OnFolderChange event in that I create a new file selection from the newly selected folder. I set the FileTypes property to this new list. But I observe that the FileTypes property remains unchanged. As a result no files are found.

Am I right to assume that the FileTypes property cannot be changed for the current TFileOpenDialog?


Solution

  • This control is a wrapper around the common item dialog. The documentation for IFileDialog::SetFileTypes says:

    This method must be called before the dialog is shown and can only be called once for each dialog instance. File types cannot be modified once the Common Item dialog box is displayed.

    So you cannot modify the file types after the dialog has been shown.

    FWIW, each time you call FileTypes.Add you are adding a new file type. You need to call FileTypes.Add once per file type.