Delphi 动态创建GridPanel

Delphi 如何动态创建GridPanel
procedure TForm12.FormCreate(Sender: TObject);
var
grdpnl: TGridPanel;
lbl: TLabel;
img: TImage;
begin
grdpnl := TGridPanel.Create(self);
grdpnl.Parent := pnl1;
grdpnl.Align := alTop;
grdpnl.Height := 38;
grdpnl.Caption := 'grdpnl1';
// grdpnl.BevelOuter := bvNone;
grdpnl.ColumnCollection.Add.SizeStyle := ssAbsolute;
grdpnl.ColumnCollection.Add.Value := 10;
grdpnl.ColumnCollection.Add.SizeStyle := ssAbsolute;
grdpnl.ColumnCollection.Add.Value := 50;
grdpnl.ColumnCollection.Add.SizeStyle := ssAbsolute;
grdpnl.ColumnCollection.Add.Value := 10;
grdpnl.ColumnCollection.Add.SizeStyle := ssPercent;
grdpnl.ColumnCollection.Add.Value := 100;
grdpnl.ColumnCollection.Add.SizeStyle := ssAbsolute;
grdpnl.ColumnCollection.Add.Value := 40;
grdpnl.RowCollection.Add.SizeStyle := ssPercent;
grdpnl.RowCollection.Add.Value := 50;
grdpnl.RowCollection.Add.SizeStyle := ssAbsolute;
grdpnl.RowCollection.Add.Value := 17;
grdpnl.RowCollection.Add.SizeStyle := ssPercent;
grdpnl.RowCollection.Add.Value := 50;
//创建lbl
lbl := TLabel.Create(grdpnl);
lbl.Parent := grdpnl;
lbl.Left := 10;
lbl.Top := 10;
lbl.Width := 50;
lbl.Height := 17;
lbl.Align := alClient;
lbl.Caption := '张自力';
lbl.Font.Name := '微软雅黑';
lbl.Font.Color := $00333333;
lbl.Font.Charset := GB2312_CHARSET;
lbl.Font.Size := 9;
lbl.Layout := tlCenter;
这样写不起作用啊,还有Label应该如何分配Colum和Row

2个回答

grdpnl: TGridPanel;
lbl: TLabel;
img: TImage;

声明放到全局去,局部的过程执行完被自动释放了。

var
grdpnl: TGridPanel;
lbl: TLabel;
img: TImage;

procedure TForm12.FormCreate(Sender: TObject);
begin
//创建代码
end;

procedure TForm12.FormDestroy(Sender: TObject);
begin
grdpnl.free;
lbl.free;
img.free;
end;

立即提问