Hi, another basic Delphi (feeling stupid) question ..

Hi, another basic Delphi (feeling stupid) question ..
I am trying to add a control at runtime to a TGridPanelLayout control. The layout panel grid is 4 x 4. Adding a control anywhere works except as position 0,0.
When I add a groupbox at position 0,0 to span 4 cols and 2 rows, it behave incorrectly. If this is the first control I add it gets created in the first cell only. If I move this code to after the correct working groupbox, then it gets created in position row0,col1 which to me is even stranger.

Any help appreciated!!

My code looks like this:

//Remember to first add a TGridPanelLayout called gplMain to the main Form.
procedure TForm1.FormCreate(Sender: TObject);
var
AControlItem: TGridPanelLayout.TControlItem;
gbox1,gbox2: TGroupBox;
begin
try
gplMain.BeginUpdate;
// Box that dont
gbox2 := TGroupBox.Create(Form1);
gbox2.Parent := gplMain;
gbox2.Text := 'Some things dont';
gbox2.Align := TAlignLayout.Client;

AControlItem := gplMain.ControlCollection.Add;
AControlItem.Column := 0;
AControlItem.Row := 0;
AControlItem.ColumnSpan := 4;
AControlItem.RowSpan := 1;
AControlItem.Control := gbox2;

// Box that works
gbox1 := TGroupBox.Create(Form1);
gbox1.Parent := gplMain;
gbox1.Text := 'Some things work';
gbox1.Align := TAlignLayout.Client;

AControlItem := gplMain.ControlCollection.Add;
AControlItem.Column := 1;
AControlItem.Row := 2;
AControlItem.ColumnSpan := 2;
AControlItem.RowSpan := 1;
AControlItem.Control := gbox1;
finally
gplMain.EndUpdate;
end;
end;

Comments

  1. Anyone have a comment on this please?

    ReplyDelete
  2. Ok, for those interested, I found my error!

    The TGridPanelayout automatically create a new item entry as soon as you set a component's parent to the gridpanellayout component.
    To get a handle on the relevant controlcollection.item I just use the controlcollection.count - 1 (assuming the last controlcollection.item is the latest.
    Hope this help someone else. :)

    ReplyDelete

Post a Comment