Link to home
Start Free TrialLog in
Avatar of ejla51
ejla51

asked on

Delphi 7 runtime created MenuItems?

Sirs,
Can not figure out how to handle events generated from runtime created
Submenus ... Trying to put tag value and Items, but looks like the event handler, that all values are at 0.
for i := 0 to ComboBox.Items.Count do // Fill Submenu from ComboBox
   begin
    NewMenuItem := TMenuItem.Create(Menu1);
    NewMenuItem.Caption := ComboBox.Items[i];
    NewMenuItem.Tag := i; {Tag with the item number}
    NewMenuItem.OnClick := MenuClick;
    Menu1.Add(NewMenuItem);
   end;

procedure TForm1.MenuClick(Sender: TObject);
begin
 // ComboBox ItemIndex shall be changed from Submenu ...
 ShowMessage('Tag = ' + IntToStr(Menu1.Tag));
 ComboBox.ItemIndex := Menu1.Tag;
 ComboBoxChange(nil);
end;

Open in new window

Avatar of 8080_Diver
8080_Diver
Flag of United States of America image

I believe that you will find that Menu1.Tag is referencing the Tag from the Menu and not from the MenuItem.  In order to get to the MenuItem you are going to have to reference Menu1.Items[Ndx].Tag.
ASKER CERTIFIED SOLUTION
Avatar of Salih Maoui
Salih Maoui
Flag of Algeria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ejla51
ejla51

ASKER

Great! Thanks for the nice lesson :)