File Open and File Save Dialogues

To provide access to the standard Windows Open File dialog box drop an OpenFileDialog control onto a form. For File/Save operations drop a SaveFileDialog control onto the form. In the example below this is tied to options in a main menu to Open and Save a file:

The Filter property of each Dialog is set to something like this:

This provides the drop down list from which the file type can be chosen. The declarations for the example are as follows:

type
 TRec=record
  name:string[20];
 end;

var
 Form1: TForm1;
 intfilename:file of TRec;
 filename:string;
 rec : TRec;

THe code for saving a file is kept very simple so the same item is saved each time:

procedure TForm1.Save1Click(Sender: TObject);
begin
 if SaveDialog1.Execute then
 begin
  filename:=SaveDialog1.FileName;
  Assignfile(intfilename, filename);
  rewrite (intfilename);
  rec.name:='Queen Victoria';
  write(intfilename,rec);
  closefile(intfilename);
 end;
end;

The code for opening a file is:

procedure TForm1.Open1Click(Sender: TObject);
begin
 if OpenDialog1.Execute then
 begin
   filename:=OpenDialog1.FileName;
   Assignfile(intfilename, filename);
   reset (intfilename);
   while not eof (intfilename) do
   begin
    read(intfilename,rec);
    memo1.lines.add (rec.name);
   end;
   closefile(intfilename);
 end;
end;

When the file is opened the contents are displayed in the memo control:

When the Open option is chosen from the File menu the Load and Save dialog boxes look like this:

(The second two options have not been picked up by Alt-PrintScreen). 

Back to Palette List