Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically creating a sub-menu in Delphi

I have a popup menu and I want one of the items to open a sub-menu with a dynamically created list (it's a list of user defined flags). Here's how I'm creating the menu items (FlagAs is the menu item I want to attach the sub-menu to):

lNewMenuItems: array[0..flagCount] of tMenuItem;

for I := 0 to flagCount do
begin
  { Create a new menu item }
  lNewMenuItems[I] := tMenuItem.Create(FlagAs);
  lNewMenuItems[I].Caption := FlagNames[I];
  lNewMenuItems[I].Tag := I; { Tag with the flag number }
  lNewMenuItems[I].OnClick := miFlagClick;
end;

FlagAs.Add(lNewMenuItems);

The miFlagClick handler simply toggles the checked status of its sender:

procedure TMyForm.miFlagClick(Sender: TObject);
begin
  (Sender as tMenuItem).Checked := not (Sender as tMenuItem).Checked;
end;

The items get added perfectly, but they don't get checked when I click on them. The event handler is being called EDIT: and Sender is the correct menu item, but the check mark doesn't appear next time I open the menu.

What am I doing wrong? Or am I going about the menu creation in the wrong way? (Note flagCount may change in the future, but is defined as a constant in the code)

EDIT: the above does actually work - see my answer below

like image 839
Mark Pim Avatar asked May 12 '26 06:05

Mark Pim


1 Answers

I tried the following in Delphi 2009 and it worked fine:

procedure TForm5.FormCreate(Sender: TObject);
var
  i : Integer;
  mis : array[0..3] of TMenuItem;
begin
  for i := 0 to 3 do begin
    mis[i] := tMenuItem.Create(SubMenu);
    NewMenu(mis[i]);
  end;
  SubMenu.Add(mis);
end;

procedure TForm5.NewMenu(var mi: TMenuItem);
begin
  mi.Caption := 'Test';
  mi.OnClick := TestClick;
end;

procedure TForm5.TestClick(Sender: TObject);
begin
 (Sender as tMenuItem).Checked := not (Sender as tMenuItem).Checked;
end;
like image 131
Toon Krijthe Avatar answered May 15 '26 07:05

Toon Krijthe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!