How can I duplicate tabPage in c#?

元气小坏坏 提交于 2020-02-03 05:39:09

问题


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.


回答1:


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);
}



回答2:


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



来源:https://stackoverflow.com/questions/17305249/how-can-i-duplicate-tabpage-in-c

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