1

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.

livelaughlove
  • 376
  • 3
  • 9
  • 21
  • 1
    maybe you need to clone the objects inside sheet? – CharlesB Apr 16 '12 at 18:27
  • i was thinking the same thing because I have done duplications of a tframe dynamically and the components and functions ends up still working. however a tabsheet is a little different. i tried to duplicate it the same way i did with tframes and it doesn't work the same. nothing in the tabsheet gets duplicated. mb i need to put everything in the tabsheet on to a tframe and add the tframe onto the new tabsheet? how would u suggest i go about doing this? – livelaughlove Apr 16 '12 at 19:13
  • Yes, placing a `TFrame` onto the `TTabSheet` would be a good solution. – Remy Lebeau Apr 18 '12 at 04:18

1 Answers1

2

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • here you are rescuing me again! ty =D – livelaughlove Apr 18 '12 at 13:39
  • I do not understand why this works. I tried this on a form and it did not work as the component names of the children remain the same, and you cannot create two components with the same name. Has this behaviour changed in Berlin? – Mike Versteeg Mar 23 '17 at 08:24
  • 1
    @MikeVersteeg two components within the same Owner cannot have the same name. Both approaches I show create a new Owner, so no duplicates. If you write a Form to the DFM and then read it back into the same Form object, then yes, you would end up with duplicates. – Remy Lebeau Mar 23 '17 at 14:34