Search code examples
delphiwindows-7fileopendialog

Delphi OpenDialog without letting the user navigate away from the inital dir


I am trying to create an open dialog (in Windows 7) where the user is confined to the initial directory. On the open dialog I have set the optionsEX to [ofExNoPlacesBar] and that removes the bar that would let them select folders and directories to go to quickly but the user can use the bread crumb address tool to go up a level and type a different directory into the filename text box to change directories.

Thank you


Solution

  • If you are using Delphi 2009+, there is a TFileOpenDialog. Use this, and set

    procedure TForm3.FileOpenDialog1FolderChange(Sender: TObject);
    begin
      FInitiated := true;
    end;
    
    procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
      var CanChange: Boolean);
    begin
      CanChange := not FInitiated;
    end;
    
    procedure TForm3.btnOpenClick(Sender: TObject);
    begin
      FInitiated := false;
      FileOpenDialog1.DefaultFolder := 'C:\MyFolder\';
      FileOpenDialog1.Execute;
    end;
    

    where

    var
      FInitiated: boolean;
    

    (Notice that there should be exactly one FInitiated per TFileOpenDialog. So, if FileOpenDialog is a private member of TForm3, let FInitiated be a private member of TForm3 as well.)

    To improve the user experience, you will probably use

    procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
      var CanChange: Boolean);
    begin
      CanChange := not FInitiated;
      if not CanChange then beep;
    end;
    

    or

    procedure TForm3.FileOpenDialog1FolderChanging(Sender: TObject;
      var CanChange: Boolean);
    begin
      CanChange := not FInitiated;
      if not CanChange then
        MessageBox(Handle, PChar('Directory selection is not allowed.'), PChar(Caption), MB_ICONINFORMATION);
    end;