delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Making a "recent files" menuComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
GUI
Skill Level:
Scoring:
Last Update:
03/08/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet THistory recent files reopen history
Times Scored:
2
Visits:
2693
Uploader: dan strandberg
Company:
Reference: N/A
Component Download: http://danstr.stud.hive.no/delphi/History.zip
 
Question/Problem/Abstract:
How to make a simple "recent files" menu/list in your application.
Answer:



A while ago I had to add a "Recent files Menu" in a project I'm working on. So I wrote a simple component that should be (A) flexible. (B) reusable in other projects. THistory was born :)

You can download THistory here. Do whatever you want with it, but I would apprechiate if you could let me know if you use it in your program :)

The goal of this article is to make a simple "reopen" menu (see image below). First some code then I'll try to explain what it does.



// Update the "Reopen" list..
procedure TfrmMain.History1Change(Sender: TObject);
var i: integer;
begin
  with Reopen1 do
  begin

    Clear;
    for i:= 0 to History1.Count-1 do
    begin
      Add(TMenuItem.Create(self));
      with Items[Count-1] do
      begin

        Caption := '&' + IntToStr(i) +' '+ History1.Items[i];
        Tag := i;
        OnClick := GenericReopenClick;
      end;
    end;

    Enabled := Count > 0;
    // Add linebreak
    Add(TMenuItem.Create(self));
    Items[Count-1].Caption := '-';
    // Add 'Clear History'
    Add( TMenuItem.Create(self) );
    with Items[Count-1] do
    begin

      Caption := 'Clear History';
      Tag := -1;
      OnClick := GenericReopenClick;
    end;
  end;
end;


// Reopen item clicked...
procedure TfrmMain.GenericReopenClick(Sender: TObject);
begin
  if
TMenuitem(Sender).Tag >= 0 then
    OpenFile(History1.Items[TMenuitem(Sender).Tag])
  else
    History1.Clear;
end;

//...

procedure TfrmMain.OpenFile(FileName: string);
begin
  // Your code here...
  History1.AddItem(FileName);
end;


Note that this is not the code for the component, you have to download and install it for this to work. History1Change is the OnChange event for my THistory object, History1. Every time an item is added to the History component this procedure gets called so we know the list have been changed. So we simply recreate the sub menu of "Reopen1" with the current items + a "Clear history" item.

GenericReopenClick, the procedure we set on all menuitems OnClick is where you would put your code to handle the event where someone clicked on an item in our menu. I thought it was easiest to save the index of every history item in the corresponding MenuItems' Tag property as you can see above. The index of an History item never can be -1 so I used it to clear the history.

THistory can only load/save it's list from/to ini files. But adding support for other formats such as the registry or a custom format shouldn't be a problem.

If you find any bugs or make any improvements, please let me know so I can update it.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)