Solved

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

Posted on 2006-11-08
7
183 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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