• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Making a 'recently used file' list in the File-menu at runtime

Dear Experts,

I want to make a 'Recent Files'-list in my File-menu, similar to what Microsoft Word and such uses for the last files you opened. I have searched and searched on this site, and I found the code below, but I don't understand how to
implement it in my application.

for i := 0 to FRecentFiles.Count - 1 do
begin
RecentFileItem := TMenuItem.Create(self);
File1.add(RecentFileItem);
//The Caption is actually cropped to what's after the last / (The filename without path)
RecentFileItem.Caption := ExtractFileName(FRecentFiles[i]);
RecentFileItem.Tag := i;
RecentFileItem.Visible := true;
RecentFileItem.OnClick := OpenRecentFile;
end;

procedure TForm1.OpenRecentFile(Sender: TObject);
begin
  OpenThisFile(FRecentFiles[TComponent(Sender).Tag]);
end;

Does someone know how?

Kind Regards,

Peter Kiers
0
peterkiers
Asked:
peterkiers
  • 10
  • 5
1 Solution
 
mikelittlewoodCommented:
Well the first thing here is to work out all the variables being used and what they are.

For example:

File1 = TMainMenu Item by the look of it
FRecentFiles maybe a TObjectList or TList, not sure.

Also there is a procedure missing for the code for OpenThisFile
0
 
mikelittlewoodCommented:
In fact, FRecentFiles seems to be a specific object, not sure if it is a custom file store object.
0
 
peterkiersAuthor Commented:
I found the example on this site with the title:

Best practise for making a 'recently used file' list in the File-menu at runtime

Peter
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mikelittlewoodCommented:
Ah ok, I found the thread you were looking at.
All it is is a TStringList holding a list of filenames and their paths.

Im guessing that the list is loading and saving itself to a registry or text file, text file would be the easiest.

You will also need a TOpenDialog component on your form to open the selected file.

Let me add some extra code, 1 sec while I write.
0
 
mikelittlewoodCommented:
btw, do you want the same list as from Windows? Or your own list via when you open from within your application?
0
 
peterkiersAuthor Commented:
My own list.

P.
0
 
mikelittlewoodCommented:
Ok here is a quick one.
Limit 10 different file names.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, Contnrs, ShellAPI;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    OpenDialog1: TOpenDialog;
    Open1: TMenuItem;
    N1: TMenuItem;
    RecentFiles1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Open1Click(Sender: TObject);
  private
    FRecentFiles: TStringList;

    procedure LoadRecentFileList;
    procedure SaveRecentFileList;
    procedure OpenRecentFile( Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FRecentFiles := TStringList.Create;
  FRecentFiles.Duplicates := dupIgnore;

  if FileExists( 'c:\MyRecentFileList.txt') then
    FRecentFiles.LoadFromFile( 'c:\MyRecentFileList.txt');

  LoadRecentFileList;
end;

procedure TForm1.LoadRecentFileList;
var
  i: Integer;
  RecentFileItem: TMenuItem;
begin
  for i := 0 to FRecentFiles.Count - 1 do
  begin
    RecentFileItem := TMenuItem.Create(self);
    RecentFiles1.add(RecentFileItem);
    //The Caption is actually cropped to what's after the last / (The filename without path)
    RecentFileItem.Caption := ExtractFileName(FRecentFiles[i]);
    RecentFileItem.Tag := i;
    RecentFileItem.Visible := true;
    RecentFileItem.OnClick := OpenRecentFile;
  end;
end;


procedure TForm1.OpenRecentFile( Sender: TObject);
var
  sFileName: String;
begin
  sFileName := FRecentFiles[ TMenuItem( Sender).Tag];

  ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);
end;

procedure TForm1.SaveRecentFileList;
begin
  FRecentFiles.SaveToFile( 'c:\MyRecentFileList.txt');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveRecentFileList;
end;

procedure TForm1.Open1Click(Sender: TObject);
var
  sFileName: String;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;

    ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);

    FRecentFiles.Insert(0, sFileName);
    if FRecentFiles.Count > 10 then
      FRecentFiles.Delete( 10);

    RecentFiles1.Clear;
    LoadRecentFileList;
  end;
end;

end.






object Form1: TForm1
  Left = 264
  Top = 160
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  Menu = MainMenu1
  OldCreateOrder = False
  OnClose = FormClose
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object MainMenu1: TMainMenu
    Left = 104
    Top = 40
    object File1: TMenuItem
      Caption = 'File'
      object Open1: TMenuItem
        Caption = 'Open'
        OnClick = Open1Click
      end
      object N1: TMenuItem
        Caption = '-'
      end
      object RecentFiles1: TMenuItem
        Caption = 'Recent Files'
      end
    end
  end
  object OpenDialog1: TOpenDialog
    Left = 232
    Top = 40
  end
end
0
 
mikelittlewoodCommented:
Slightly made it a bit more clever


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, Contnrs, ShellAPI;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    OpenDialog1: TOpenDialog;
    Open1: TMenuItem;
    N1: TMenuItem;
    RecentFiles1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Open1Click(Sender: TObject);
  private
    FRecentFiles: TStringList;

    procedure LoadRecentFileList;
    procedure SaveRecentFileList;
    procedure OpenRecentFile( Sender: TObject);
    procedure ReloadFileList( sFileName: String);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FRecentFiles := TStringList.Create;
  FRecentFiles.Duplicates := dupIgnore;

  if FileExists( 'c:\MyRecentFileList.txt') then
    FRecentFiles.LoadFromFile( 'c:\MyRecentFileList.txt');

  LoadRecentFileList;
end;

procedure TForm1.LoadRecentFileList;
var
  i: Integer;
  RecentFileItem: TMenuItem;
begin
  for i := 0 to FRecentFiles.Count - 1 do
  begin
    RecentFileItem := TMenuItem.Create(self);
    RecentFiles1.add(RecentFileItem);
    //The Caption is actually cropped to what's after the last / (The filename without path)
    RecentFileItem.Caption := ExtractFileName(FRecentFiles[i]);
    RecentFileItem.Tag := i;
    RecentFileItem.Visible := true;
    RecentFileItem.OnClick := OpenRecentFile;
  end;
end;


procedure TForm1.OpenRecentFile( Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  sFileName := FRecentFiles[ TMenuItem( Sender).Tag];

  i := FRecentFiles.IndexOf( sFileName);

  FRecentFiles.Delete( i);
  ReloadFileList( sFileName);

  ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);
end;

procedure TForm1.SaveRecentFileList;
begin
  FRecentFiles.SaveToFile( 'c:\MyRecentFileList.txt');

  FreeAndNil( FRecentFiles);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveRecentFileList;
end;

procedure TForm1.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;

    ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);

    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
      FRecentFiles.Delete( i);

    ReloadFileList( sFileName);
  end;
end;

procedure TForm1.ReloadFileList(sFileName: String);
begin
  FRecentFiles.Insert(0, sFileName);
  if FRecentFiles.Count > 10 then
    FRecentFiles.Delete( 10);

  RecentFiles1.Clear;
  LoadRecentFileList;
end;

end.
0
 
mikelittlewoodCommented:
Im heading out now so won't see any more responses.
Hopefully this will give you most of what you need.
Ill check back later tonight to see if you need anything else.

Good luck with your coding.
0
 
peterkiersAuthor Commented:
Wauw, I am impressed.

I need a little time to try your example.

Peter
0
 
peterkiersAuthor Commented:
Your example works fine.
Only one little question:

Is it possible that the recent files appears as menu-items instead as a submenu-items?

file
  open
  recentfiles > documen1
                     document2
                             etc...
instead

file
open
--------------
document1
document2
 etc...
--------------

Peter
0
 
mikelittlewoodCommented:
Yeah let me adjust it slightly, Ive just got to make the delete current recent file from the first menu item without deleting the Open menuitem as I made it easy by using the .clear call to the previous menuitem. Cant do this on the file one as it would delete the open one.
0
 
mikelittlewoodCommented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ShellAPI;

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    OpenDialog1: TOpenDialog;
    File1: TMenuItem;
    Open1: TMenuItem;
    N1: TMenuItem;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Open1Click(Sender: TObject);
  private
    { Private declarations }
  public
    FRecentFiles: TStringList;

    procedure LoadRecentFileList;
    procedure SaveRecentFileList;
    procedure OpenRecentFile( Sender: TObject);
    procedure ReloadFileList( sFileName: String);
    procedure ClearList;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

procedure TForm1.LoadRecentFileList;
var
  i: Integer;
  RecentFileItem: TMenuItem;
begin
  for i := 0 to FRecentFiles.Count - 1 do
  begin
    RecentFileItem := TMenuItem.Create(self);
    File1.add( RecentFileItem);
    //The Caption is actually cropped to what's after the last / (The filename without path)
    RecentFileItem.Caption := ExtractFileName(FRecentFiles[i]);
    RecentFileItem.Tag := i;
    RecentFileItem.Visible := true;
    RecentFileItem.OnClick := OpenRecentFile;
    RecentFileItem.Name := 'Recent' + IntToStr( i);
  end;
end;

procedure TForm1.ClearList;
var
  i: Integer;
  FMenuItem: TMenuItem;
begin
  for i := 0 to FRecentFiles.Count - 1 do
  begin
    FMenuItem := TMenuItem( FindComponent( 'Recent' + IntToStr( i)));
    FreeAndNil( FMenuItem);
  end;
end;

procedure TForm1.OpenRecentFile(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  sFileName := FRecentFiles[ TMenuItem( Sender).Tag];

  i := FRecentFiles.IndexOf( sFileName);

  FRecentFiles.Delete( i);
  ReloadFileList( sFileName);

  ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);
end;

procedure TForm1.ReloadFileList(sFileName: String);
begin
  FRecentFiles.Insert(0, sFileName);
  if FRecentFiles.Count > 10 then
    FRecentFiles.Delete( 10);

  ClearList;
  LoadRecentFileList;
end;

procedure TForm1.SaveRecentFileList;
begin
  FRecentFiles.SaveToFile( 'c:\MyRecentFileList.txt');

  FreeAndNil( FRecentFiles);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FRecentFiles := TStringList.Create;
  FRecentFiles.Duplicates := dupIgnore;

  if FileExists( 'c:\MyRecentFileList.txt') then
    FRecentFiles.LoadFromFile( 'c:\MyRecentFileList.txt');

  LoadRecentFileList;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveRecentFileList;
end;

procedure TForm1.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;

    ShellExecute(Handle, 'open', PChar( sFilename), nil, nil, SW_SHOWNORMAL);

    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
      FRecentFiles.Delete( i);

    ReloadFileList( sFileName);
  end;
end;

end.
0
 
peterkiersAuthor Commented:
Mike, your example works fine.

Thanks for your help.

5OO p's comming your way...

Greetings,

Peter Kiers
0
 
mikelittlewoodCommented:
Glad I could help, sorry for the slow reply, just crawled out of bed  :o)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now