Mega Code Archive

 
Categories / Delphi / VCL
 

Dynamic Pagecontrols

Title: Dynamic Pagecontrols Question: How can I create a TPageControl and the corresponding TTabSheets dynamically? Answer: The trick is the correct parenting. Tabsheets belong to a certain Pagecontrol. You have to set the Pagecontrol property of the Tabsheet to tell Delphi where to place your Tabsheet (you can use this property to move a tabsheet from one pagecontrol to another one). Sample code: procedure TForm1.Button1Click(Sender: TObject); var PGC: TPageControl; tab1, tab2: TTabSheet; begin PGC := TPageControl.Create(self); with PGC do begin Parent := self; Top := 20; Left := 40; Height := 100; Width := 300; end; tab1 := TTabSheet.Create(PGC); with tab1 do begin Visible := true; Caption := 'Dynamic Tabsheet'; PageControl := PGC; end; tab2 := TTabSheet.Create(PGC); with tab2 do begin Visible := true; Caption := 'Dynamic Tabsheet - Sheet 2'; PageControl := PGC; end; with TButton.Create(self) do begin Parent := tab2; Left := 10; Top := 10; Caption := 'A Button'; end; // if you've got more than one tabsheet, you'll have to cylce them trough once. // otherwise, the pagecontrol doesn't initialize properly. PGC.ActivePage := tab2; PGC.ActivePage := tab1; end;