Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

different panelwidth when aligning with code

Hi,
i want to adjust all document panels side by side with code:

  w := Site.RootZone.Width div DocumentCount;
  for i := ComponentCount - 1 downto 0 do
  begin
    if Components[i] is TLMDDockPanel then
    begin
      pnl := TLMDDockPanel(Components[i]);
      if copy(pnl.Name, 1, 12) = 'LMDDockPanel' then
      begin
          Site.DockControl(pnl, Site.RootZone, alLeft);
          pnl.Zone.Width := w;
      end;
    end;
  end;

But all document windows get different width :-(
What is wrong with my code?

Comments

  • 8 Comments sorted by Votes Date Added
  • Things can get more complicated here. Try to use the following code:

    function TForm3.GetPanels: TArray<TLMDDockPanel>;
    var
      lst: TList<TLMDDockPanel>;
      i: Integer;
    begin
      lst := TList<TLMDDockPanel>.Create;
      try
        for i := 0 to ComponentCount - 1 do
          if Components[i] is TLMDDockPanel then
            lst.Add(TLMDDockPanel(Components[i]));
        Result := lst.toArray;
      finally
        lst.Free;
      end;
    end;

    procedure TForm3.ShowPanels(const APanels: TArray<TLMDDockPanel>;
      AShow: Boolean);
    var
      pnl: TLMDDockPanel;
    begin
      for pnl in APanels do
        pnl.PanelVisible := AShow;
    end;

    procedure TForm3.Button1Click(Sender: TObject);
    var
      i:    Integer;
      pnl:  TLMDDockPanel;
      wd:   Integer;
      pnls: TArray<TLMDDockPanel>;
    begin
      pnls := GetPanels;
      ShowPanels(pnls, False);

      for pnl in pnls do
        LMDDockSite1.DockControl(pnl, LMDDockSite1.RootZone, alLeft);

      wd := LMDDockSite1.RootZone.Width div Length(pnls);
      for pnl in pnls do
        pnl.Zone.Width := wd;

      ShowPanels(pnls, True);
    end;
  • Despite not related to DockingPack itself, I want to mention, that your code:

    w := Site.RootZone.Width div DocumentCount;

    Is not correct, because not every site width is divisible by panel count. I mean that  w * DocumentCount willl not be equal to Site.RootZone.Width, because of integer division.
    You have to use some floating point arithmetic here (with rounding to integer panel widths). I guess you'll realize it after trying the simpler original way.
  • edited June 2020 Posts: 13Vote Up0Vote Down
    Thank you, your code works with a little problem:

    If i make the main-window smaller, all panels will be smaller, ok, 
    but if i make the main-window wider, all panels keep their width and don't grow.

    Stefan
  • Because all of them are now aligned to the left.  You can align one of them to alClient and then this panel will be expanded on window expand.
  • If you want all panels to be expanded uniformly, you have to set Site.UseSpace property to False. Updated code:

    procedure TForm3.Button1Click(Sender: TObject);
    var
      pnl:  TLMDDockPanel;
      sw:   Integer;
      wd:   Integer;
      pnls: TArray<TLMDDockPanel>;
      szs:  TArray<Integer>;
      i:    Integer;
    begin
      pnls := GetPanels;
      ShowPanels(pnls, False);

      for pnl in pnls do
        LMDDockSite1.DockControl(pnl, LMDDockSite1.RootZone, alLeft);

      sw := LMDDockManager1.DockElems.HorzZone.SplitterWidth.Value[dv100];
      wd := (LMDDockSite1.RootZone.Width - sw * High(pnls)) div Length(pnls);

      SetLength(szs, Length(pnls));
      for i := 0 to High(szs) do
        szs[i] := wd;

      ShowPanels(pnls, True);
      LMDDockSite1.RootZone.SetChildrenSizes(szs);
    end;
  • edited June 2020 Posts: 13Vote Up0Vote Down
    Thank you, but this doesn't work!

    If i make the mainform smaller, all panels get smaller, but if i make the mainform wider, all panels dont grow...
    If i drag panels with mouse and drop them side by side, the panels will be greater or smaller when i change mainform with mouse.
    I would like to set this up exactly by code.

    Stefan
  • I've already mentioned this aspect. IF you want panels to widen you generally have two options:

    1) The code shows aligning all panels to the left. But, you can align one of them as alClient, so allowing it to widen and fill all remaining area.
    2) You can switch off site's SpaceZone concept setting Site.UseSpace property to False. In this case panels will expand uniformly, but UseSpace is applicable to the whole site.

    Its generally hard to say, what is your setup. Can you send a demo along with screeenshots (including small and large window sizes) and steps to reproduce of how you achieve what you need with mouse. I just want to look at resulting layout.
  • I have changed your code to 

      for pnl in pnls do
        Site.DockControl(pnl, Site.RootZone, alDocLeft);

    and now all works fine ;-)

    Stefan

Sign In or Register to comment.