8

How can I duplicate a "tabPage" inside of my TabControl?

I tried this:

   //My TabControl: tc
   //My Tab ID: 0
   TabPage newPage = new TabPage();

   foreach (Control control in tc.TabPages[0].Controls)
   {
      newPage.Controls.Add(control);
   }
   tc.TabPages.Add(newPage);

but it doesn't work.

Thanks in advance.

Olian04
  • 6,480
  • 2
  • 27
  • 54
Lucas
  • 1,514
  • 3
  • 16
  • 23
  • 3
    You need to _copy_ all of the controls. This is not simple; consider moving them to a UserControl. – SLaks Jun 25 '13 at 18:52
  • @SLaks, I'm looking for [this](http://stackoverflow.com/questions/14507173/copy-tabcontrol-tab) article, it's a right way? – Lucas Jun 27 '13 at 17:43

2 Answers2

13

I got it!

For those who has the same kind of problem, Here is what I’ve done:

I had created a UserControl (thanks a lot for @SLaks and @Brian for your tip), copied all objects from my TabControl to my new UserControl and used the follow code to create a dynamic tabs:

for (int x = 0; x < 3; x++)
{
   UserControl1 uc = new UserControl1();
   TabPage tp = new TabPage();
   tp.Controls.Add(uc);
   this.TabControl1.TabPages.Add(tp);
}
Brian
  • 5,069
  • 7
  • 37
  • 47
Lucas
  • 1,514
  • 3
  • 16
  • 23
2

As Schabse mentioned in a comment above, I highly recommend that you do this with User Controls.

Brian
  • 5,069
  • 7
  • 37
  • 47