Solved

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

Posted on 2006-11-04
15
220 Views
Last Modified: 2010-04-05
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
Comment
Question by:peterkiers
  • 10
  • 5
15 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
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
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
In fact, FRecentFiles seems to be a specific object, not sure if it is a custom file store object.
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
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
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
btw, do you want the same list as from Windows? Or your own list via when you open from within your application?
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
My own list.

P.
0
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
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
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
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
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
Wauw, I am impressed.

I need a little time to try your example.

Peter
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
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
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 500 total points
Comment Utility
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
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
Mike, your example works fine.

Thanks for your help.

5OO p's comming your way...

Greetings,

Peter Kiers
0
 
LVL 15

Expert Comment

by:mikelittlewood
Comment Utility
Glad I could help, sorry for the slow reply, just crawled out of bed  :o)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now