How to have an absolute row between two percentage rows when adding to a TGridPanel from code?

822 Views Asked by At

I am trying to create a Form with a TGridPanel from code.

It contains:

  • A memo at the top (which is set to 50%)
  • A navigator at the center (which is set to 24 pixels)
  • A grid at the bottom (which is set to 50%)

This is the code I wrote:

uses
  Winapi.Messages, Winapi.Windows, System.Classes, System.SysUtils,
  System.UITypes, System.Variants, Vcl.Controls, Vcl.DBCtrls, Vcl.DBGrids,
  Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Forms, Vcl.Graphics, Vcl.Grids, Vcl.StdCtrls,
  Data.DB;

procedure Test;
var
  View: TForm;
  GridPanel: TGridPanel;
  Grid: TDBGrid;
  DataSource: TDataSource;
  Navigator: TDBNavigator;
  Memo: TMemo;
begin
  View := TForm.Create(Application);
  try
    View.Name := 'Form2';
    // SystemFont(View.Font);
    View.Width := 640;
    View.Height := 480;
    View.Position := TPosition.poOwnerFormCenter;
    GridPanel := TGridPanel.Create(View);
    GridPanel.Name := 'GridPanel';
    GridPanel.Caption := '';
    GridPanel.BevelOuter := TBevelCut.bvNone;
    GridPanel.FullRepaint := False;
    GridPanel.Parent := View;
    GridPanel.Align := TAlign.alClient;

    GridPanel.ColumnCollection.BeginUpdate;
    GridPanel.ColumnCollection.Delete(1);
    GridPanel.ColumnCollection[0].Value := 100;
    GridPanel.ColumnCollection.EndUpdate;
    GridPanel.RowCollection.BeginUpdate;
    GridPanel.RowCollection.Add;
    TCellItem(GridPanel.RowCollection[0]).Value := 50;
    TCellItem(GridPanel.RowCollection[0]).SizeStyle := TSizeStyle.ssPercent;
    TCellItem(GridPanel.RowCollection[1]).Value := 24;
    TCellItem(GridPanel.RowCollection[1]).SizeStyle := TSizeStyle.ssAbsolute;
    TCellItem(GridPanel.RowCollection[2]).Value := 50;
    TCellItem(GridPanel.RowCollection[2]).SizeStyle := TSizeStyle.ssPercent;
    GridPanel.RowCollection.EndUpdate;

    Memo := TMemo.Create(View);
    Memo.Name := 'Memo';
    Memo.Parent := GridPanel;
    Memo.Lines.Clear;
    Memo.Align := TAlign.alClient;
    DataSource := TDataSource.Create(View);
    Navigator := TDBNavigator.Create(View);
    Navigator.Name := 'Navigator';
    Navigator.DataSource := DataSource;
    Navigator.Parent := GridPanel;
    Navigator.Align := TAlign.alClient;
    Grid := TDBGrid.Create(View);
    Grid.Name := 'Grid';
    Grid.Parent := GridPanel;
    Grid.Align := TAlign.alClient;
    Grid.DataSource := DataSource;

    {
    GridPanel.ControlCollection.BeginUpdate;
    GridPanel.ControlCollection.AddControl(Memo, 0, 0);
    GridPanel.ControlCollection.AddControl(Navigator, 0, 1);
    GridPanel.ControlCollection.AddControl(Grid, 0, 2);
    GridPanel.ControlCollection.EndUpdate;
    }

    // ShowMessage(ComponentToString(View));
    View.ShowModal;
  finally
    View.Free;
  end;
end

The result looks like this:

enter image description here

The Problem: There is a gap at the bottom of the form and no DB navigator to be seen!

A dump of the DFM looks fine to me:

object Form2: TForm
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 441
  ClientWidth = 624
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  Position = poOwnerFormCenter
  PixelsPerInch = 96
  TextHeight = 13
  object GridPanel: TGridPanel
    Left = 0
    Top = 0
    Width = 624
    Height = 441
    Align = alClient
    BevelOuter = bvNone
    ColumnCollection = <
      item
        Value = 100.000000000000000000
      end>
    ControlCollection = <
      item
        Column = 0
        Control = Memo
        Row = 0
      end
      item
        Column = 0
        Control = Navigator
        Row = 1
      end
      item
        Column = 0
        Control = Grid
        Row = 2
      end>
    FullRepaint = False
    RowCollection = <
      item
        Value = 50.000000000000000000
      end
      item
        SizeStyle = ssAbsolute
        Value = 24.000000000000000000
      end
      item
        Value = 50.000000000000000000
      end>
    TabOrder = 0
    object Memo: TMemo
      Left = 0
      Top = 0
      Width = 624
      Height = 208
      Align = alClient
      TabOrder = 0
      ExplicitLeft = 219
      ExplicitTop = 59
      ExplicitWidth = 185
      ExplicitHeight = 89
    end
    object Navigator: TDBNavigator
      Left = 0
      Top = 208
      Width = 624
      Height = 18
      Align = alClient
      TabOrder = 1
      ExplicitTop = 0
      ExplicitWidth = 240
      ExplicitHeight = 25
    end
    object Grid: TDBGrid
      Left = 0
      Top = 208
      Width = 624
      Height = 209
      Align = alClient
      TabOrder = 2
      TitleFont.Charset = DEFAULT_CHARSET
      TitleFont.Color = clWindowText
      TitleFont.Height = -11
      TitleFont.Name = 'Tahoma'
      TitleFont.Style = []
    end
  end
  object TDataSource
  end
end

When I change the position, the Navigator is at the correct position, but I want it to be absolute.

TCellItem(GridPanel.RowCollection[1]).Value := 5;
TCellItem(GridPanel.RowCollection[1]).SizeStyle := TSizeStyle.ssPercent;

Why does TGridPanel behave so strange in this case? What can I do about it?

1

There are 1 best solutions below

2
On BEST ANSWER

Change the order of setting SizeStyle and Value.

TCellItem(GridPanel.RowCollection[0]).SizeStyle := TSizeStyle.ssPercent;
TCellItem(GridPanel.RowCollection[0]).Value := 50;
TCellItem(GridPanel.RowCollection[1]).SizeStyle := TSizeStyle.ssAbsolute;
TCellItem(GridPanel.RowCollection[1]).Value := 24;
TCellItem(GridPanel.RowCollection[2]).SizeStyle := TSizeStyle.ssPercent;
TCellItem(GridPanel.RowCollection[2]).Value := 50;