I have a popup menu and I want one of the elements to open a submenu with a dynamically created list (this is a list of user-defined flags). Here, as I create the menu items ( FlagAs- this is the menu item to which I want to add a submenu):
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 handler miFlagClicksimply switches the checked state of its sender:
procedure TMyForm.miFlagClick(Sender: TObject);
begin
(Sender as tMenuItem).Checked := not (Sender as tMenuItem).Checked;
end;
Elements are added perfectly, but they are not checked when I click on them. The event handler is called EDIT: and Sender is the correct menu item, but the checkmark does not appear the next time I open the menu.
What am I doing wrong? Or am I going to create a menu incorrectly? (Note flagCountmay change in the future, but is defined as a constant in the code)
: - .