Search code examples
delphidelphi-xevcl

How do I make two controls each occupy half their parent's area?


I have an application that has a sidebar attached to it (TPanel --> alRight), that uses a CategoryPanel (alClient) inside of it. This CategoryPanel has exactly 2 Groups which are non aligned. I would like to share the boundary of these 2 groups so it would fill up the whole panel space in a 50/50 ratio. Unfortunately The CategoryGroups do not support alignment in designtime, which forces me to run my application every time I want to test it. I tried to set each CategoryGroup to the height to half of the panel, but it displays scrollbars. (See Picture 2)

How can I align/share the boundary in a ratio of 50/50 properly?

Image1 Image2


Solution

  • According to your comments, you want to run this code:

    procedure TForm1.UpdateGroupHeights;
    begin
      if not CategoryPanel1.Collapsed then
        CategoryPanel1.Height := CategoryPanelGroup1.ClientHeight div 2;
      if not CategoryPanel2.Collapsed then
        CategoryPanel2.Height := CategoryPanelGroup1.ClientHeight -
          CategoryPanelGroup1.ClientHeight div 2;
    end;
    

    whenever anything changes that you wish to affect the layout of your groups. So I think you need to call this function from the following events:

    • The OnCreate event of the form.
    • The OnResize event of the TCategoryPanelGroup.
    • The OnCollapse and OnExpand events of the two category panels.

    That looks a bit weird though when one panel is collapsed, and the other is expanded. Personally I'd rejig the code to fill all available space.

    if not CategoryPanel1.Collapsed then
      ;//nothing to do
    if CategoryPanel1.Collapsed and not CategoryPanel2.Collapsed then
      CategoryPanel2.Height := CategoryPanelGroup1.ClientHeight-CategoryPanel1.Height;
    if not CategoryPanel1.Collapsed and CategoryPanel2.Collapsed then
      CategoryPanel1.Height := CategoryPanelGroup1.ClientHeight-CategoryPanel2.Height;
    if not CategoryPanel1.Collapsed and not CategoryPanel2.Collapsed then
    begin
      CategoryPanel1.Height := CategoryPanelGroup1.ClientHeight div 2;
      CategoryPanel2.Height := CategoryPanelGroup1.ClientHeight-CategoryPanel1.Height;
    end;