Dynamically add tab sheets to page control and embed a form?

ⅰ亾dé卋堺 提交于 2020-01-12 06:57:26

问题


I'm working on a module which consists of a page control. By default, this page control (TPageControl) shouldn't have any tab sheets (TTabSheet), but upon initialization, it should dynamically insert these pages and embed a form inside of it.

The issue comes with knowing how to insert a tab sheet into the page control. How do I create this? And once it's created, along with the forms inside each one, how do I iterate through them to destroy the forms?


回答1:


1. How to dynamically create a tab sheet ?

procedure TForm1.Button1Click(Sender: TObject);
var
  TabSheet: TTabSheet;
begin
  TabSheet := TTabSheet.Create(PageControl1);
  TabSheet.Caption := 'New Tab Sheet';
  TabSheet.PageControl := PageControl1;
end;

2. How to embed a form inside of a tab sheet ?

To insert a form inside of a tab sheet use simply a parent change:

Form2.Parent := TabSheet;
Form2.Show;

3. Will I need to manually free the forms embedded into a tab sheet when destroying it ?

No, it is enough to free a tab sheet. In case when the forms will have a tab sheet, or to be more precise, the TWinControl as their Parent, that parent will take care of their release when freeing itself.




回答2:


David Heffernan is right.

Form2.Parent := TabSheet;
Form2.Show;

This code just means Form2's parent is TabSheet, not it's owner.

You can create the form like this:

Form2 := TForm2.Create(nil);

and then free it by yourself. or you can create a form like this:

Form2 := TForm2.Create(Form1);

Form1 is the owner of Form2, and it will automatically free Form2 when itself is freed.



来源:https://stackoverflow.com/questions/12233205/dynamically-add-tab-sheets-to-page-control-and-embed-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!