Issues with GridPanel in Delphi XE

722 Views Asked by At

The GridPanel has issues! Am I doing something wrong? First row at column 2 is a bug on resizing the form. Tested with Delphi XE 6 and 10.2.2! Place a TGridPanel to the Form and set "Align" to "alClient". Start and resize form.

Try the following code:

procedure TForm5.Button1Click(Sender: TObject);
var
  Col,Row: Integer;
  CI: TControlItem;
  Panel: TPanel;
  Rows, Cols: Integer;
begin
  GridPanel1.RowCollection.BeginUpdate;
  GridPanel1.ColumnCollection.BeginUpdate;
  GridPanel1.ColumnCollection.BeginUpdate;

  GridPanel1.RowCollection.Clear;
  GridPanel1.ColumnCollection.Clear;

  Rows := 6;
  Cols := 4;

  for Row := 1 to Rows do
  begin
    with GridPanel1.RowCollection.Add do
    begin
      SizeStyle := ssPercent;
      Value := 100 / Rows;
    end;
  end;

  for Col := 1 to Cols do
  begin
    with GridPanel1.ColumnCollection.Add do
    begin
      SizeStyle := ssPercent;
      Value := 100 / Cols;
    end;
  end;

  for Row := 0 to GridPanel1.RowCollection.Count - 1 do
  begin
    for Col := 0 to GridPanel1.ColumnCollection.Count - 1 do
    begin
      Panel := TPanel.Create(Self);
      Panel.Parent := GridPanel1;
      CI := GridPanel1.ControlCollection.Add;
      CI.Column := Col;
      CI.Row := Row;
      Panel.Caption := 'Row ' + Row.ToString + ' Col ' + Col.ToString;
      CI.Control := Panel;
    end;
  end;
  GridPanel1.ColumnCollection.EndUpdate;
  GridPanel1.RowCollection.EndUpdate;
  GridPanel1.ColumnCollection.EndUpdate;
end;
1

There are 1 best solutions below

1
Brian On

As pointed out by Tom Brunberg setting the Panel.Parent puts them in the control collection and manages things.

  for Row := 0 to GridPanel1.RowCollection.Count - 1 do
  begin
    for Col := 0 to GridPanel1.ColumnCollection.Count - 1 do
    begin
      Panel := TPanel.Create(Self);
      Panel.Parent := GridPanel1;
      Panel.Caption := 'Row ' + Row.ToString + ' Col ' + Col.ToString;
    end;
  end;

Your code makes the collection end up with 48 controls instead of 24.

form1.Caption := 'ControlCollection.Count:' + IntToStr(GridPanel1.ControlCollection.Count);