troubleshooting Question

RecentFileList with the numbering underlined.

Avatar of Peter Kiers
Peter KiersFlag for Netherlands asked on
Delphi
7 Comments1 Solution299 ViewsLast Modified:
Dear Experts,

I have code in the code-section to make a RecentFileList.
The code works perfect and I have it just the way I want it.
Except for one little detail. I want the numbering of the
RecentFileList underlined, just lik MSWORD has.

Does someone know how?

P.
unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, FileCtrl;  // <---Add FileCtrl

type
  TMainForm = class(TForm)
    OpenDialog1: TOpenDialog;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Open1: TMenuItem;
    N1: TMenuItem;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Open1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure ExitFile(Sender: TObject);
  public
    { Public declarations }
    FRecentFiles: TStringList;
    procedure updateRecent(sFileName: string);
    procedure LoadRecentFileList;
    procedure SaveRecentFileList;
    procedure OpenRecentFile(Sender: TObject);
    procedure ReloadFileList(sFileName: string);
    procedure ClearList;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

{ TForm1 }

procedure TMainForm.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 TMainForm.ExitFile(Sender: TObject);
begin
  Close
end;
(*---------------------------------------------------*)
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveRecentFileList;
end;
(*---------------------------------------------------*)
procedure TMainForm.FormCreate(Sender: TObject);
begin
  Caption := 'Untitled - My Application';
  FRecentFiles := TStringList.Create;
  FRecentFiles.Duplicates := dupIgnore;
  if FileExists(ExtractFilePath(Application.ExeName) + 'MyRecentFileList.txt') then
    FRecentFiles.LoadFromFile(ExtractFilePath(Application.ExeName) + 'MyRecentFileList.txt');
  LoadRecentFileList;
end;
(*---------------------------------------------------*)
procedure TMainForm.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+1) + '  ' + (MinimizeName(FRecentFiles[i],Canvas,150));
      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 := 'E&xit';
    ExMenuItem.Visible := true;
    ExMenuItem.Hint := 'Quits the application';
    ExMenuItem.Name := 'ExitMenuItem';
    ExMenuItem.OnClick := ExitFile;
  end;
end;
(*---------------------------------------------------*)
procedure TMainForm.Open1Click(Sender: TObject);
var
  sFileName: string;
begin
  if OpenDialog1.Execute then
  begin
    sFileName := OpenDialog1.FileName;
    MainForm.Caption := ExtractFileName(OpenDialog1.FileName) + ' - ' + 'My Application';
    updateRecent(sFileName);
  end;
end;
(*---------------------------------------------------*)
procedure TMainForm.OpenRecentFile(Sender: TObject);
var
  sFileName: string;
  i: Integer;
begin
  sFileName := FRecentFiles[TMenuItem(Sender).Tag];
  i := FRecentFiles.IndexOf(sFileName);
  FRecentFiles.Delete(i);
  ReloadFileList(sFileName);
  MainForm.Caption := ExtractFileName(sFileName) + ' - ' + 'My Application';
end;
(*---------------------------------------------------*)
procedure TMainForm.ReloadFileList(sFileName: string);
begin
  FRecentFiles.Insert(0, sFileName);
  if FRecentFiles.Count > 7 then
    FRecentFiles.Delete(7);
  ClearList;
  LoadRecentFileList;
end;
(*---------------------------------------------------*)
procedure TMainForm.SaveRecentFileList;
begin
  FRecentFiles.SaveToFile(ExtractFilePath(Application.ExeName) + 'MyRecentFileList.txt');
  FreeAndNil(FRecentFiles);
end;
(*---------------------------------------------------*)
procedure TMainForm.updateRecent(sFileName: string);
var i: integer;
begin
  i := FRecentFiles.IndexOf(sFileName);
  if i >= 0 then
    FRecentFiles.Delete(i);
  ReloadFileList(sFileName);
end;
(*---------------------------------------------------*)
end.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros