[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

RecentFileList with the numbering underlined.

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.

Open in new window

0
peterkiers
Asked:
peterkiers
1 Solution
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
What about using hotkeys?
RecentFileItem.Caption := '&'+IntToStr(i+1) + '  ' + (MinimizeName(FRecentFiles[i],Canvas,150));

Open in new window

0
 
epasquierCommented:
Exactly. To complement Ferrucio68 solution, here is an explanation.
It's a simple thing in Delphi Menus : you can define the shortcut key automatically by adding the '&' character before the one that will be used as a shortcut, directly in the caption
It appears with an underline character, but it's not the only thing.
if, when this part of the menu is visible, you strike the corresponding key, then the menu item is executed
0
 
peterkiersAuthor Commented:
Thanks for the tip.
Peter
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
peterkiersAuthor Commented:
When I open Word, and go to the mainmenu you see all the hotkeys.
In my application I have to press the ALT-key to see my hotkeys.
What do I have to do to display my hotkeys at startup?

Greetings, Peter Kiers
0
 
Geert GruwezOracle dbaCommented:
this thread should help you:
http://www.tek-tips.com/viewthread.cfm?qid=1489404&page=22

basically it's a windows setting
there is also a delphi override feature
0
 
Geert GruwezOracle dbaCommented:
if you choose for the delphi option
you'll have to double click the top menu item
and edit the AdvancedDrawItem with the code from the tek-tips thread
then attach all the other AdvancedDrawItem too to this event handler
0
 
peterkiersAuthor Commented:
OK.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now