Search code examples
delphidelphi-xe

Find parent of the submenu items in delphi


I want to find out parent when i click on the submenu item. For example in the image below when i click on L3B the result should be "L1/L2/L3B".

Popup menu


Solution

  • You can use a recursive function to get the full path of your menu.

    function Form1.GetMenuPath(Menu: TMenuItem): String;
    begin
      if (Menu.Parent <> nil) and (Menu.Parent.ClassType = TMenuItem) then
        Result := GetMenuPath(TMenuItem(Menu.Parent));
      if Result <> '' then
        Result := Result + ' > ';
      Result := Result + Menu.Caption;
    end;
    

    At your MenuItemClick you call the function

    procedure Form1.L3B1Click(Sender: TObject);
    begin
      ShowMessage(GetMenuPath(TMenuItem(Sender)));
    end;