2

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;
Snitch
  • 51
  • 1
  • Hmm, What is the issue? Are you talking about Vcl or Fmx. In the title you speak about Delphi XE, in the text about Delphi XE6 and 10.2.2. You have added a tag `delphi-xe`. So which version do you refer to? What is the parent of the button if the `TGridPanel.Align = alClient` ? I guess, the grid panel, but does it matter for your question in which panel? – Tom Brunberg Oct 11 '18 at 15:00
  • In your loop, the panel creation loop, replace `CI.Column := Col; CI.Row := Row;` with `CI.Column := 0; CI.Row := 0;` – Sertac Akyuz Oct 11 '18 at 15:15
  • @Tom - The issue is the 3rd sentence *"First row at column 2 is a bug on resizing..."*. Evident when you run the code, some cells do not follow suit. The parent of the button is the form since the code first removes all cells. You can "bring to front" the button if it is obscured. – Sertac Akyuz Oct 11 '18 at 15:19

1 Answers1

2

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);
Brian
  • 6,717
  • 2
  • 23
  • 31
  • 1
    Actually, delete the line which creates the `CI` and everything that is related to `CI`. The controlcollection is managed internally. – Tom Brunberg Oct 11 '18 at 15:51