0

This question was previously asked about 3 years ago but only half answered.

The original question is here: How To Read and Write Delphi 2010 RibbonApplicationMenuBar Recent Items To A File and the accepted answer gives enough information so that you can figure out how to save the mru list but no information on how to successfully reload it.

This is what I have at the moment.

SaveDocHistory() is called by the closing event on the form and does what it is supposed to.

procedure TfrmMain.SaveDocHistory;
var
  ini : TIniFile;
  i : Integer;
begin
  Ini := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  try
    ini.EraseSection('DocHistory');
    ini.WriteString('DocHistory', 'FileCount', IntToStr(RibbonApplicationMenuBar1.RecentItems.Count));

    for i := 0 to Pred(RibbonApplicationMenuBar1.RecentItems.Count) do
      ini.WriteString('DocHistory', 'File' + IntToStr(i),
                      RibbonApplicationMenuBar1.RecentItems.Items[i].Caption );
  finally
    ini.Free;
  end;
end;

The resultant ini file entries look like this:

[DocHistory]
FileCount=3
File0=F:\Projects\StevenTest\Test02.rtf
File1=F:\Projects\StevenTest\Test1.rtf
File2=F:\Projects\StevenTest\Test01.rtf

The problem is reloading the RibbonApplicationMenuBar's RecentItems list. My code 'appears' to work in that in the items are listed in the recently used section of the application - but unlike 'real' mru items they do not response to the click event. Any new items added to the mru list work but not my 'implants'. So it appears that I'm reloading the captions but not the file information. Here is my code, a function that's called by the FormCreate event of the form holding the RibbonApplicationMenuBar.

procedure TfrmMain.LoadDocHistory;
var
  ini : TIniFile;
  nCount, i : Integer;
  sTmp : string;
begin
  Ini := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  try
    nCount := ini.ReadInteger('DocHistory', 'FileCount', 0 );

    for i := 0 to Pred(nCount) do
    begin
      sTmp := ini.ReadString('DocHistory', 'File' + IntToStr(i), '');
      RibbonApplicationMenuBar1.RecentItems.Add.Caption := sTmp;
    end;

  finally
    ini.Free;
  end;
end;

I've tried deciphering Delphi's help but the AddRecentItem() function method they mention either is not applicable to the RibbonApplicationMenuBar or I have been incorrect in the way I tried to use it. I have searched the web but all I've found are multiple references to the previously mentioned thread. If anyone has been successful in getting the 2nd half of this issue resolved I would appreciate the information.

BTW like the title of the post I am using Delphi2010 for this project.

Thanks..

Community
  • 1
  • 1
TheSteven
  • 900
  • 8
  • 23
  • possible duplicate of [How To Read and Write Delphi 2010 RibbonApplicationMenuBar Recent Items To A File](http://stackoverflow.com/questions/1402373/how-to-read-and-write-delphi-2010-ribbonapplicationmenubar-recent-items-to-a-fil) – David Heffernan Oct 28 '12 at 11:28
  • Please don't knowingly ask a question that is an exact duplicate. Instead either edit an answer at the original question or supply your own. I recommend that you add the useful code as a new answer to the original question. And then delete this. – David Heffernan Oct 28 '12 at 11:30
  • At the time I entered this - I didn't knowingly ask the question. The original question had been idle for over 3 years and only 1/2 of it was answered so it was time to re-ask it in hope of getting the full answer. I just happened to find the answer shortly after posting this. – TheSteven Oct 28 '12 at 22:16
  • It's still a dupe according to the rules of the game. – David Heffernan Oct 28 '12 at 22:25

1 Answers1

4

While I was checking my post for typos I thought of something which turned out to be the solution. Here's the answer...

I needed to load the data instead to the ribbon1 sub-component. Looks like it's working fine now.

procedure TfrmMain.LoadDocHistory;
var
  ini : TIniFile;
  nCount, i : Integer;
  sTmp : string;
begin
  Ini := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  try
    nCount := ini.ReadInteger('DocHistory', 'FileCount', 0 );

    for i := 0 to Pred(nCount) do
    begin
      sTmp := ini.ReadString('DocHistory', 'File' + IntToStr(i), '');
      Ribbon1.AddRecentItem(sTmp);  //<<<<<<<<<<
    end;
  finally
    ini.Free;
  end;
end;

Now granted there could be a FileExists() check and other error handling but what I needed right now was to get this working.

TheSteven
  • 900
  • 8
  • 23