Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on 

When I save a file with SaveDialog the recentfile list doesn't update

Hi, in my Notepad application I have created a Recentfile list.
And the code works like a clock. I have put the code in the Code Snippet.

I have made a MainMenu procedure called Open1Click and it look like this:

procedure TfrmTextToSpeech.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;
    reText.Lines.LoadFromFile(OpenDialog1.FileName);
    frmTextToSpeech.Caption := ExtractFileName(OpenDialog1.FileName) + ' - ' + 'Text2Speech';
    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
    FRecentFiles.Delete( i);
    ReloadFileList( sFileName);
  end;
end;

The code above opens a file and updates the recenfile list (the last 4 lines).

But when add the last 4 lines to the MainMenu procedure SaveFileAsText (uses a SaveDialog ofcaurse)
the recentfile list doesn't update. This is what the procedure looks like:

procedure TfrmTextToSpeech.SaveFileAsTextClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
  begin
    Screen.Cursor := crHourGlass;
    StatusBar1.SimpleText := 'Saving...';
    reText.Lines.SaveToFile(SaveDialog1.FileName);
    frmTextToSpeech.Caption := ExtractFileName(SaveDialog1.FileName) + ' - ' + 'Text2Speech';
    StatusBar1.SimpleText := 'Ready';
    Screen.Cursor := crDefault;
   end;
end;

I can't figure out why? Can somebody help me to solve this problem?

Peter

Private
 
  fRecentFiles: TStringList;
------------------------------------------------------*)
procedure TfrmTextToSpeech.OpenRecentFile(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  sFileName := FRecentFiles[ TMenuItem( Sender).Tag];
  i := FRecentFiles.IndexOf( sFileName);
  FRecentFiles.Delete( i);
  ReloadFileList( sFileName);
  reText.Lines.LoadFromFile(sFileName);
  frmTextToSpeech.Caption := ExtractFileName(SFilename) + ' - ' + 'Text2Speech';
end;
(*-----------------------------------------------------------------*)
procedure TfrmTextToSpeech.ReloadFileList(sFileName: String);
begin
  FRecentFiles.Insert(0, sFileName);
  if FRecentFiles.Count > 7 then
    FRecentFiles.Delete( 7);
  ClearList;
  LoadRecentFileList;
end;
(*-----------------------------------------------------------------*)
procedure TfrmTextToSpeech.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveRecentFileList;
end;
(*-----------------------------------------------------------------*)
procedure TfrmTextToSpeech.FormCreate(Sender: TObject);
begin
  FRecentFiles := TStringList.Create;
  FRecentFiles.Duplicates := dupIgnore;
  if FileExists( ExtractFilePath(Application.ExeName) + 'MyRecentFileList.txt') then
  FRecentFiles.LoadFromFile( ExtractFilePath(Application.ExeName) + 'MyRecentFileList.txt');
  LoadRecentFileList;
end;
(*-----------------------------------------------------------------*)
procedure TfrmTextToSpeech.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 TfrmTextToSpeech.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 TfrmTextToSpeech.ExitFile(Sender: TObject);
begin
  Close;
end;
(*-----------------------------------------------------------------*)
procedure TfrmTextToSpeech.SaveRecentFileList;
begin
   FRecentFiles.SaveToFile( ExtractFilePath(Application.ExeName) + 'MyRecentFileList.txt');
   FreeAndNil( FRecentFiles);
end;
(*-----------------------------------------------------------------*)

Open in new window

Delphi

Avatar of undefined
Last Comment
Peter Kiers
Avatar of 2266180
2266180
Flag of United States of America image

you need to place the code from open to save as well :)
procedure TfrmTextToSpeech.updateRecent(sFileName:string);
var i:integer;
begin
    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
      FRecentFiles.Delete( i);
    ReloadFileList( sFileName);
end;
 
procedure TfrmTextToSpeech.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;
    reText.Lines.LoadFromFile(OpenDialog1.FileName);
    frmTextToSpeech.Caption := ExtractFileName(OpenDialog1.FileName) + ' - ' + 'Text2Speech';
    updateRecent(sFileName);
  end;
end;
 
procedure TfrmTextToSpeech.SaveFileAsTextClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
  begin
    Screen.Cursor := crHourGlass;
    StatusBar1.SimpleText := 'Saving...';
    reText.Lines.SaveToFile(SaveDialog1.FileName);
    frmTextToSpeech.Caption := ExtractFileName(SaveDialog1.FileName) + ' - ' + 'Text2Speech';
    StatusBar1.SimpleText := 'Ready';
    Screen.Cursor := crDefault;
    updateRecent(sFileName);// HERE
   end;
end;

Open in new window

Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

Hi Ciuly you have gave me the right solutions, the 500p, are yours anyway.
But i have a little question to you about the sFilename: string. So give me a couple of minutes...
Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

I get undeclared identifier sFilename in procedure SaveFileAsText
I quess when i declare sFilename globally the problem is solved.
But i have a question to you there are 5 procedures that
uses variable sFilename:string also and there not declared globally!
Is this right?

I have put the 5 procedures in a Code Snippet

Greetings,

P.


procedure TfrmTextToSpeech.Open1Click(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;
    reText.Lines.LoadFromFile(OpenDialog1.FileName);
    frmTextToSpeech.Caption := ExtractFileName(OpenDialog1.FileName) + ' - ' + 'Text2Speech';
    updateRecent(sFileName);
  end;
end;
 
procedure TfrmTextToSpeech.OpenRecentFile(Sender: TObject);
var
  sFileName: String;
  i: Integer;
begin
  sFileName := FRecentFiles[ TMenuItem( Sender).Tag];
  i := FRecentFiles.IndexOf( sFileName);
  FRecentFiles.Delete( i);
  ReloadFileList( sFileName);
  reText.Lines.LoadFromFile(sFileName);
  frmTextToSpeech.Caption := ExtractFileName(SFilename) + ' - ' + 'Text2Speech';
end;
 
procedure TfrmTextToSpeech.ReloadFileList(sFileName: String);
begin
  FRecentFiles.Insert(0, sFileName);
  if FRecentFiles.Count > 7 then
    FRecentFiles.Delete( 7);
  ClearList;
  LoadRecentFileList;
end;
 
procedure TfrmTextToSpeech.SaveFileAsTextClick(Sender: TObject);
begin
  if SaveDialog1.Execute then
  begin
    Screen.Cursor := crHourGlass;
    StatusBar1.SimpleText := 'Saving...';
    reText.Lines.SaveToFile(SaveDialog1.FileName);
    frmTextToSpeech.Caption := ExtractFileName(SaveDialog1.FileName) + ' - ' + 'Text2Speech';
    StatusBar1.SimpleText := 'Ready';
    Screen.Cursor := crDefault;
    updateRecent(sFileName);// HERE
   end;
end;
 
procedure TfrmTextToSpeech.updateRecent(sFileName: string);
var i:integer;
begin
    i := FRecentFiles.IndexOf( sFileName);
    if i >= 0 then
      FRecentFiles.Delete( i);
    ReloadFileList( sFileName);
end;

Open in new window

Avatar of 2266180
2266180
Flag of United States of America image

I just copied that from your code as it was. and moved the common code to a new procedure (which you will have to declare in private section of your form; forgot to mention this :P )
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

sorry, do not understand
Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

i understand now
Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

I have test the code and it works like a clock.
Thank you very much and 500 comming your way...

Greetings,

Peter Kiers
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo