Delphi XE中的GridPanel问题

时间:2018-10-11 14:42:00

标签: delphi delphi-xe delphi-xe6 gridpanel

GridPanel出现问题! 难道我做错了什么? 第2列的第一行是调整表单大小的错误。经过Delphi XE 6和10.2.2的测试! 将TGridPanel放置在窗体上,并将“ Align”设置为“ alClient”。启动并调整表格大小。

尝试以下代码:

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 个答案:

答案 0 :(得分:2)

正如汤姆·布鲁伯格(Tom Brunberg)指出的那样,设置面板。父对象将它们放入控件集合中并进行管理。

  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;

您的代码使集合最终有48个控件,而不是24个。

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