How to dynamically duplicate a tab?

1.8k views Asked by At

I have a TPageControl with a TTabSheet. and in that TTabSheet i have a bunch of functions and components. I would like to duplicate that tabSheet at run time via a button with all the functions and components still in it and working.

Right now I managed to duplicate the tabsheet. However, the new tabsheet is completely empty.

Here is my code for that button.

TTabSheet * NewTabSheet= new TTabSheet(pageControlMain);
NewTabSheet->PageControl = pageControlMain;
NewTabSheet->Caption = "TabSheet";
pageControlMain->ActivePage = NewTabSheet;

What am I missing?

As for the components and functions inside the TTabSheets, they're just scrollboxes, edits, buttons, and panels.

1

There are 1 answers

3
Remy Lebeau On BEST ANSWER

The TTabSheet class by itself does not have any child controls, that is why you don't see anything. You have to instantiate each individual control and copy their data as well.

One way to do that is to use the TStream.WriteComponent() and TStream.ReadComponent() methods to save the source TTabSheet into a temporary DFM and then load that into the new TTabSheet, eg:

TMemoryStream *Strm = new TMemoryStream;
Strm->WriteComponent(SourceTabSheet);
Strm->Position = 0;
TTabSheet *NewTabSheet = new TTabSheet(pageControlMain);
NewTabSheet->PageControl = pageControlMain;
Strm->ReadComponent(NewTabSheet);
pageControlMain->ActivePage = NewTabSheet;
delete Strm;

Another option is to place your components onto a TFrame-derived class at design-time, then create an instance of that class at run-time and place it onto each TTabSheet, letting it handle the controls for you, eg:

TTabSheet *NewTabSheet = new TTabSheet(pageControlMain);
NewTabSheet->PageControl = pageControlMain;
TMyFrame *NewFrame = new TMyFrame(NewTabSheet);
NewFrame->Parent = NewTabSheet;
pageControlMain->ActivePage = NewTabSheet;