Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TPopupMenu, Delphi 7


This question is not answered.


Permlink Replies: 2 - Last Post: Jun 4, 2015 10:20 AM Last Post By: Remy Lebeau (Te...
Jim Sawyer

Posts: 214
Registered: 1/3/10
TPopupMenu, Delphi 7  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 2, 2015 1:12 PM
I'm using a TPopupMenu which will have two options( "month", and "year" ). Since I want the "year" to have a submenu of years ('2000", "2001", etc), but want to start with twenty years ago until current, this must be done at design time since twenty years ago won't be the same year next year as this. I have no idea how to code the addition of the twenty submenu options. Can someone enlighten me, preferably with a bit of code for this old redneck brain to digest?

Thanks,
Jim Sawyer
DataWare
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TPopupMenu, Delphi 7
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 2, 2015 2:13 PM   in response to: Jim Sawyer in response to: Jim Sawyer
Jim wrote:

want to start with twenty years ago until current, this must be done at
design time since twenty years ago won't be the same year next year as
this.

I think you mean "run time" instead of "design time".

I have no idea how to code the addition of the twenty submenu options.

You need to populate the submenu dynamically at run-time. Use the NewItem()
function to create the menu items, and the TMenuItem.Add() method to add
them to the submenu. For example:

uses
  ..., DateUtils;
 
procedure TMyForm.PopupMenu1Popup(Sender: TObject);
var
  dt: TDateTime;
  Yr, I: Integer;
  Items: array[0..19] of TMenuItem;
begin
  YearMenu.Clear;
  dt := IncYear(Date, -20);
  Yr := YearOf(dt);
  for I := 0 to 19 do
    Item[I] := NewItem(IntToStr(Yr + I), 0, false, true, YearClicked, 0, '');
  YearMenu.Add(Items);
end;
 
procedure TMyForm.YearClicked(Sender: TObject);
var
  Yr: Integer;
begin
  Yr := TMenuItem(Sender).Tag;
  // use Yr as needed...
end;


--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TPopupMenu, Delphi 7  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 4, 2015 10:20 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
for I := 0 to 19 do
Item[I] := NewItem(IntToStr(Yr + I), 0, false, true, YearClicked, 0, '');
YearMenu.Add(Items);

Sorry, I left out an important piece:

for I := 0 to 19 do
begin
  Item[I] := NewItem(IntToStr(Yr + I), 0, false, true, YearClicked, 0, '');
  Item[I].Tag := Yr + I; // <-- add this
end;
YearMenu.Add(Items);


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02