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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With