Solved

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

Posted on 2006-11-08
7
179 Views
Last Modified: 2010-04-05
Hi,

A while a go I have asked how to implement Recent files in my mainmenu.
I received a good solution. As always I close the question to soon.

I have put the example on my site, click on the floppy-disk beneath the
Under Construction bar to download the example.

My question:

In the example when files has been opened it look like this:

Open
---------------
recent file1
recent file2
recent file3
recent file4


But I need the menu-item Exit beneath the recent files and a seperator. Like this:

Open
---------------
recent file1
recent file2
recent file3
recent file4
---------------
exit

And if its possible auto numbering the recent files: like this:
Open
---------------
1. recent file1
2. recent file2
3. recent file3
4. recent file4
---------------
exit

Who want to help me, because i can't do it on my own.
Another change to earn 500 points.

Kind Regards,

Peter Kiers
0
Comment
Question by:peterkiers
  • 4
  • 3
7 Comments
 
LVL 28

Expert Comment

by:TName
ID: 17898583
Hi, try this:


type
  TForm1 = class(TForm)
   ...
  private
    procedure ExitFile(Sender: TObject);
  public
    ...
 





procedure TForm1.LoadRecentFileList;
var
  i: Integer;
  RecentFileItem: TMenuItem;
  LFileItem, ExMenuItem: TMenuItem;

begin
  for i := 0 to FRecentFiles.Count - 1 do
  begin
    if Trim(FRecentFiles[i])<>'' then begin
      RecentFileItem := TMenuItem.Create(self);
      File1.add( RecentFileItem);
      RecentFileItem.Caption := IntToStr(i)+'   '+ExtractFileName(FRecentFiles[i]);
      RecentFileItem.Tag := i;
      RecentFileItem.Visible := true;
      RecentFileItem.OnClick := OpenRecentFile;
      RecentFileItem.Name := 'Recent' + IntToStr( i);
    end;
  end;
  LFileItem := TMenuItem.Create(self);
  File1.Add(LFileItem);
  LFileItem.Caption := '-';
  LFileItem.Visible := True;

  ExMenuItem := TMenuItem.Create(self);
  File1.add(ExMenuItem);
  ExMenuItem.Caption := 'Exit';
  ExMenuItem.Visible := true;
  ExMenuItem.OnClick := ExitFile;
end;
(*--------------------------------------------------*)


procedure TForm1.ExitFile(Sender: TObject);
begin
  Close;
end;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17898682
Almost...

When I open files i get this:

open
--------------------
Exit                            <======  ?????
 recent file1
 recent file2
--------------------
Exit

Peter
0
 
LVL 28

Expert Comment

by:TName
ID: 17898850
>When I open files i get this:

Yes, I've fixed that, hadn't checked what the whole program does, but first have a look at this, all recent files in a submenu. If you prefer the version without the submenu, just say so and I'll post the fix for the mentioned problem :)


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    N1: TMenuItem;
    OpenDialog1: TOpenDialog;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Open1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FRecentFiles: TStringList;
    procedure LoadRecentFileList;
    procedure SaveRecentFileList;
    procedure OpenRecentFile( Sender: TObject);
    procedure ReloadFileList( sFileName: String);
    procedure ClearList;
    procedure ExitFile(Sender: TObject);

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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;
  FMenuItem := TMenuItem( FindComponent('ExitMenuItem'));
  FreeAndNil( FMenuItem);
  FMenuItem := TMenuItem( FindComponent('RecFilesItem'));
  FreeAndNil( FMenuItem);
  FMenuItem := TMenuItem( FindComponent('MenuLine1'));
  FreeAndNil( FMenuItem);
end;

(*--------------------------------------------------*)
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SaveRecentFileList;
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.LoadRecentFileList;
var
  i: Integer;

  RecentFileItem, SubRecentFileItem: TMenuItem;
  LFileItem, ExMenuItem: TMenuItem;

begin

  if TMenuItem(FindComponent('RecFilesItem'))=nil then begin
    RecentFileItem := TMenuItem.Create(self);
    File1.add(RecentFileItem);
    RecentFileItem.Name:='RecFilesItem';
    RecentFileItem.Caption :='Recent Files';
  end;
  for i := 0 to FRecentFiles.Count - 1 do
  begin
    if Trim(FRecentFiles[i])<>'' then begin
      SubRecentFileItem := TMenuItem.Create(self);
      RecentFileItem.add(SubRecentFileItem);
      SubRecentFileItem.Caption := IntToStr(i)+'  '+ExtractFileName(FRecentFiles[i]);
      SubRecentFileItem.Tag := i;
      SubRecentFileItem.Visible := true;
      SubRecentFileItem.OnClick := OpenRecentFile;
      SubRecentFileItem.Name := 'Recent' + IntToStr( i);
    end;
  end;
  if TMenuItem(FindComponent('ExitMenuItem'))=nil then begin
    LFileItem := TMenuItem.Create(self);
    File1.Add(LFileItem);
    LFileItem.Caption := '-';
    LFileItem.Visible := True;
    LFileItem.Name:='MenuLine1';
  end;

  if TMenuItem(FindComponent('ExitMenuItem'))=nil then begin
    ExMenuItem := TMenuItem.Create(self);
    File1.add(ExMenuItem);
    ExMenuItem.Caption := 'Exit';
    ExMenuItem.Visible := true;
    ExMenuItem.Name:='ExitMenuItem';
    ExMenuItem.OnClick := ExitFile;
  end;
end;
(*--------------------------------------------------*)


procedure TForm1.ExitFile(Sender: TObject);
begin
  Close;
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.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;
(*--------------------------------------------------*)
end.
0
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

 
LVL 1

Author Comment

by:peterkiers
ID: 17898905
Prefer the version without the submenu.

P.
0
 
LVL 28

Accepted Solution

by:
TName earned 500 total points
ID: 17898977
>Prefer the version without the submenu.
Ok:


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    N1: TMenuItem;
    OpenDialog1: TOpenDialog;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Open1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    FRecentFiles: TStringList;
    procedure LoadRecentFileList;
    procedure SaveRecentFileList;
    procedure OpenRecentFile( Sender: TObject);
    procedure ReloadFileList( sFileName: String);
    procedure ClearList;
    procedure ExitFile(Sender: TObject);

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

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;
  FMenuItem := TMenuItem( FindComponent('ExitMenuItem'));
  FreeAndNil( FMenuItem);
  FMenuItem := TMenuItem( FindComponent('RecFilesItem'));
  FreeAndNil( FMenuItem);
  FMenuItem := TMenuItem( FindComponent('MenuLine1'));
  FreeAndNil( FMenuItem);
end;

(*--------------------------------------------------*)
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
SaveRecentFileList;
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.LoadRecentFileList;
var
  i: Integer;
  RecentFileItem: TMenuItem;
  LFileItem, ExMenuItem: TMenuItem;
begin
  for i := 0 to FRecentFiles.Count - 1 do
  begin
    if Trim(FRecentFiles[i])<>'' then begin
      RecentFileItem := TMenuItem.Create(self);
      File1.add(RecentFileItem);
      RecentFileItem.Caption := IntToStr(i)+'  '+ExtractFileName(FRecentFiles[i]);
      RecentFileItem.Tag := i;
      RecentFileItem.Visible := true;
      RecentFileItem.OnClick := OpenRecentFile;
      RecentFileItem.Name := 'Recent' + IntToStr( i);
    end;
  end;
  if TMenuItem(FindComponent('ExitMenuItem'))=nil then begin
    LFileItem := TMenuItem.Create(self);
    File1.Add(LFileItem);
    LFileItem.Caption := '-';
    LFileItem.Visible := True;
    LFileItem.Name:='MenuLine1';
  end;

  if TMenuItem(FindComponent('ExitMenuItem'))=nil then begin
    ExMenuItem := TMenuItem.Create(self);
    File1.add(ExMenuItem);
    ExMenuItem.Caption := 'Exit';
    ExMenuItem.Visible := true;
    ExMenuItem.Name:='ExitMenuItem';
    ExMenuItem.OnClick := ExitFile;
  end;
end;
(*--------------------------------------------------*)


procedure TForm1.ExitFile(Sender: TObject);
begin
  Close;
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.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;
(*--------------------------------------------------*)
end.
0
 
LVL 28

Expert Comment

by:TName
ID: 17899067
BTW, if you want the menu items to be numbered 1 2 3...  rather than 0 1 2...  then replace this line

RecentFileItem.Caption := IntToStr(i)+'  '+ExtractFileName(FRecentFiles[i]);

with this line
RecentFileItem.Caption := IntToStr(i+1)+'  '+ExtractFileName(FRecentFiles[i]);

in the procedure LoadRecentFileList;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 17899211
Yip, that's what i ment.

500p comming your way.

Kind Regards,

Peter Kiers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

20 Experts available now in Live!

Get 1:1 Help Now