title: Programatically enable a menu item (and all it's parents) - Delphi
body: <p>I need to selectively enable certain menu items based upon the status of the user. I've managed to get code to enable the actual items I want but I can't see how to enable all the parent menu items above each one that I enable in a multi-level menu. Without enabling them as well the menu item still can't be used as the user cannot reach it.</p>
  
  <p>eg if I have</p>
  
  <pre><code>EditTop
     EditSub1
     Editsub2
        EditSubSub1
     EditSub3 
  </code></pre>
  
  <p>I can enable EditSubSub1 but I also therefore need to enable Editsub2 and EditTop as well or it can't be reached by the user. That's what I would appreciate help with.</p>
  
  <p>The code I have at the moment is the following (Assume that other code has given me a TstringList containing the menu names I want enabled)</p>
  
  <p>First some code to disable everything.</p>
  
  <pre><code>procedure DisableMenu(AMenu: TMenuItem);
  //recurses through all the menu and disables eveything 
  var
    i: integer;
  begin
  for i := 0 to AMenu.Count - 1 do
     begin
     AMenu[i].enabled := false;
     DisableMenu(AMenu[i]);
     end;
  end;
  </code></pre>
  
  <p>Then code that searches for and returns a TmenuItem based upon its name
  (This came from
   <a href="http://www.delphipages.com/forum/showthread.php?t=45723" rel="nofollow">http://www.delphipages.com/forum/showthread.php?t=45723</a>)</p>
  
  <pre><code>function FindMnuItem(Menu: TMenu; MenuName: string): TMenuItem;
  
        procedure FindSubItems(mnuItem: TMenuItem);
        var i: integer;
        begin
          for i:=0 to mnuItem.Count- 1 do
            if mnuItem.Items[i].Name= MenuName then
               begin
               Result:= mnuItem.Items[i];
               break;
               end
            else
               FindSubItems(mnuItem.Items[i]);
        end;
  
  var i: integer;
  begin
  Result:= nil;
  for i:= 0 to Menu.Items.Count -1 do
      begin
      if Menu.Items[i].name = MenuName then
         begin
         Result:= Menu.Items[i];
         break;
         end
      else
         if Result&lt;&gt; nil then
            break
         else
            FindSubItems(Menu.Items[i]);
      end;
  end;
  </code></pre>
  
  <p>Finally the code I would like some help with. This selectively enables each  menu item based upon the names in the Stringlist AllowedMenus but only those ones, not the ones above each one in the tree. How do I do that?</p>
  
  <pre><code>//first disable all menu items
  DisableMenu(MainMenu1.Items); 
  //now enable the ones we want enabled       
  for i := 0 to AllowedMenus.count-1 do
      begin
      MenuName := AllowedMenus[i];
      FindMnuItem(MainMenu1, MenuName).Enabled := true; //enable an item   
      end
  </code></pre>