Search code examples
delphidelphi-7

Delphi 7 opendialog has garbage in filename when used in Windows 10


This is my first post here so please forgive me if I am not doing it right.

I am using Delphi 7 on my Windows 10 machine. When I use the TOpenDialog I get garbage in the filename property on close. This is what I get back þƒ‡uÔÁ™ßðæRw. I created a simple form with a button and a edit box to show the problem here. Could someone please assist me.

The code is below.

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      opendialog1.Execute();
    end;

    procedure TForm1.OpenDialog1Close(Sender: TObject);
    begin
      edit1.Text := opendialog1.FileName;
    end;

Solution

  • Don't use the OnClose event of the dialog. That is invoked after the underlying dialog object, which owns the file name data, has been destroyed.

    Instead respond to the dialog when Execute returns.

    procedure TForm1.Button1Click(Sender: TObject);
    begin 
      if opendialog1.Execute() then
        edit1.Text := opendialog1.FileName;
    end;
    

    Note that you must also test the return value of Execute to handle the user cancelling the dialog.