Peter Martin's Delphi Tips

Add a Most-recently Used (MRU) Menu to your Application

An MRU menu provides fast access when re-opening recently used files, instead of having to remember where files are located and searching for them every time with an open file dialog. 

This is another case where a simple addition to your code will let your application remember things the user shouldn't have to remember. 

An MRU list is typically added to an application's File menu, or as a sub-menu under the menu item File > Reopen. 

The easiest way to implement an MRU menu is to use the TovcMenuMRU component that comes with Turbopower's Orpheus library (now available free of charge). 

This component provides everything you need except for the code to:

 Update the MRU menu when a new file is opened or saved

 Save the MRU list at the end of each session

 Restore the MRU list at the start of each session, and

 Open the right file when the user selects an item from the MRU menu

Here is an example of the trivial amount of code needed:

uses
  IniFiles;

procedure TForm1.OvcMenuMRU1Click(Sender: TObject; const ItemText: String;
    var Action: TOvcMRUClickAction);
begin
  DoSomething(ItemText);
end; 

procedure TForm1.Open1Click(Sender: TObject);
begin
  ovcMenuMRU1.Add(Filename);
end; 

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TIniFile.Create(ChangeFileExt(Application.EXEName, '.ini')) do
    try
      ovcMenuMRU1.Items.CommaText:= ReadString('Config', 'MRUList', '');
    finally
      Free;
    end;
end;


procedure TForm1.FormClose(Sender: TObject);
begin
  with TIniFile.Create(ChangeFileExt(Application.EXEName, '.ini')) do
    try
      WriteString('Config', 'MRUList', ovcMenuMRU1.Items.CommaText);
    finally
      Free;
    end;
end;