How To Read and Write Delphi 2010 RibbonApplicationMenuBar Recent Items from/to a file Part2

StackOverflow https://stackoverflow.com/questions/13106786

  •  14-07-2021
  •  | 
  •  

質問

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..

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top