Search code examples
delphifreepascallazarusz-order

freepascal/delphi components not appearing (z-order issue I think)


I had a PANEL on the form. Then dynamically I create many more panels and place them on the form to look like a menu.

By mistake I deleted the main PANEL. then put it back as a new control.

Now the dynamic buttons don't show. But there's no error. So I'm guessing that the dynamic components are there but invisible (behind the main PANEL).

Is there a way to fix this? I can't seem to find a z-order property for the components.

Please help.

Here's the code segment:

  For MenuItemCount:= 1 to MenuItemLimit Do
      Begin
      MenuButtons[MenuItemCount] := TPanel.Create(Self);
      With MenuButtons[MenuItemCount] do
          begin

            Width:=180 - (10*MenuItem[MenuItemCount].Level);
            Left:=4+10*MenuItem[MenuItemCount].Level;

            Height:=25;
            Top:= 5 + Height * (MenuItemCount-1);
            Color:= clMenu;
            Cursor:=crHandPoint;

            Parent := MenuGroup; //Parent container for the items.

            Caption := MenuItem[MenuItemCount].Title;

      end;//End for

MenuGroup is the parent panel that is placed at design-time.


Solution

  • There is the method SendToBack, which lets you send a control to the back (and its BringToFront counterpart).

    But I think it won't solve your problem. The 'Z-order' of components by default is the order in which they are created. The design time panel is created before the dynamic panels, even once you have removed it and put a new one on the form.

    What I think happened (though it's a hard guess without seeing your code), is that you tried to find the panel by name, like this:

    var
      ParentPanel: TPanel;
      DynamicPanel: TPanel;
    begin
      ParentPanel := FindComponent('PanelX') as TPanel;
    
      DynamicPanel := TPanel.Create(Self);
      DynamicPanel.Parent := ParentPanel;
    

    This would work, but if you remove PanelX, and put in a new panel with a slightly different name, FindComponent won't find the panel and return nil. The DynamicPanels will have nil as a parent, causing them not to show up.