Using Dialogues for Opening and Saving Files

We use both the execute function and the filename property of the OpenDialog and SaveDialog to handle the files. The filter property is handy for showing (in this case) only the text files and for adding the .txt extension if necessary.

The SaveToFile method of the string grid saves the contents of all the cells as an xml file.
Output from program FileDialogDemo

Output from program FileDialogDemo

The essential code of the files needed for program FileDialogDemo follows. You can download the three files for Lazarus in file_dialog_demo.zip.

uFileDialogDemo.pas

unit uFileDialogDemo;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Grids,
  StdCtrls;

type
  TfrmTimetable = class(TForm)
    btnOpen : TButton;
    btnSave : TButton;
    btnSaveAs : TButton;
    btnClear : TButton;
    OpenDialog1 : TOpenDialog;
    SaveDialog1 : TSaveDialog;
    sgTimetable : TStringGrid;
    procedure btnClearClick(Sender: TObject);
    procedure btnOpenClick(Sender: TObject);
    procedure btnSaveAsClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  end; 

var
  frmTimetable : TfrmTimetable;
  CurrentPathname : string = 'timetable.txt';

implementation

{$R *.lfm}

procedure TfrmTimetable.btnSaveAsClick(Sender: TObject);
begin
  with SaveDialog1 do
    begin
      if Execute then
        begin
          InitialDir := '';  //current directory (folder)
          CurrentPathname := Filename; //the path you navigate to plus the name you type
          {Add the extension if not present so that you can find
           the file easily with the filter set}
          if pos('.', CurrentPathname ) < 1 then
            CurrentPathname := CurrentPathname + '.txt';
          sgTimetable.SaveToFile(CurrentPathname);
          Showmessage(CurrentPathname);
        end;
    end;
end;

procedure TfrmTimetable.btnSaveClick(Sender: TObject);
begin
  sgTimetable.SaveToFile(CurrentPathname);
end;

procedure TfrmTimetable.btnClearClick(Sender: TObject);
begin
  //Clear cells from Col 1, Row 1 to Col 2 Row 2.
  sgTimetable.Clean(1, 1, 2, 2,[]);
end;

procedure TfrmTimetable.btnOpenClick(Sender: TObject);
begin
  with OpenDialog1 do
    begin
      if Execute then
        begin
          InitialDir := '';  //current folder
          CurrentPathname := Filename;
          sgTimetable.LoadFromFile(CurrentPathname);
        end;
    end;
end;

end.
  

uFileDialogDemo.lfm

object frmTimetable: TfrmTimetable
  Left = 582
  Height = 160
  Top = 205
  Width = 366
  Caption = 'Timetable'
  ClientHeight = 160
  ClientWidth = 366
  LCLVersion = '0.9.30'
  object sgTimetable: TStringGrid
    Left = 8
    Height = 92
    Top = 8
    Width = 336
    ColCount = 3
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goSmoothScroll]
    RowCount = 3
    TabOrder = 0
    ColWidths = (
      114
      104
      105
    )
    Cells = (
      4
      0
      1
      'Periods 1 and 2'
      0
      2
      'Periods 3 and 4'
      1
      0
      'Mon'
      2
      0
      'Tues'
    )
  end
  object btnOpen: TButton
    Left = 8
    Height = 25
    Top = 104
    Width = 75
    Caption = 'Open'
    OnClick = btnOpenClick
    TabOrder = 1
  end
  object btnSave: TButton
    Left = 88
    Height = 25
    Top = 104
    Width = 75
    Caption = 'Save'
    OnClick = btnSaveClick
    TabOrder = 2
  end
  object btnSaveAs: TButton
    Left = 176
    Height = 25
    Top = 104
    Width = 75
    Caption = 'SaveAs'
    OnClick = btnSaveAsClick
    TabOrder = 3
  end
  object btnClear: TButton
    Left = 272
    Height = 25
    Top = 104
    Width = 75
    Caption = 'Clear'
    OnClick = btnClearClick
    TabOrder = 4
  end
  object OpenDialog1: TOpenDialog
    Filter = 'Text file|*.txt|All files|*.*'
    left = 64
    top = 128
  end
  object SaveDialog1: TSaveDialog
    Filter = 'Text file|*.txt|All files|*.*'
    left = 272
    top = 128
  end
end
  

FileDialogDemo.lpr

program FileDialogDemo;

uses
  Interfaces, Forms, uFileDialogDemo;

begin
  Application.Initialize;
  Application.CreateForm(TfrmTimetable, frmTimetable);
  Application.Run;
end.
        
        
Programming - a skill for life!

How to use message, file, colour, font and calendar dialogue boxes to interact with the user