How to duplicate TabPage during run-time ?

G

Guest

Hello gurus,

I want to have a Form with a TabControl, this TabControl will contain
TabPage that has controls in it, this TabPage is prepared at design time.

I want in runtime to duplicate the TabPage that wad made during the design
time together with all its controls, but without the controls data.

Is there an easy way to do that?

Any sample will be highly appreciated.
 
C

Colby Africa

Something like this might work:

TabPage newPage = new TabPage();

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

tabControl1.TabPages.Add(newPage);

Colby
 
P

Peter Duniho

Something like this might work:

TabPage newPage = new TabPage();

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

tabControl1.TabPages.Add(newPage);

Off the top of my head, I don't think it will. A Control instance can
only have one parent. Adding a control taken from one TabPage will
remove it from that TabPage in the process of adding it to the new
TabPage instance.

That said, you should be able to encapsulate the initialization of a
given tab page into a custom class. I don't recall if the VS Designer
makes this easy, but the worst-case scenario should be to take the code
the Designer generates and cut-and-paste it into a new custom class,
bypassing the Designer-generated code for actual initialization. Then
you can just instantiate that custom class each time.

Even easier would be to put all of the things you want in the TabPage
into a single UserControl. The VS Designer does allow for easy
creation and design of a UserControl, and you can drag a UserControl
into any TabPage at design time _and_ also instantiate one at any time
in your code. So adding a new TabPage would be as simple as
instantiating a new blank TabPage, a new instance of the UserControl to
go with it, adding that UserControl to the TabPage and adding the
TabPage to the TabControl.

Pete
 
C

Colby Africa

Yes, that's right. What would be needed is sort of a control.clone()
operation. Thanks Pete.

Colby
 
P

Peter Duniho

Yes, that's right. What would be needed is sort of a control.clone()
operation. Thanks Pete.

If the Control class already had a Clone() method, that would
definitely be the way to go, I agree. But implementing Clone() oneself
would be, I think, more of a hassle than just allowing new instances to
be easily instantiated as I suggested.

Pete
 
G

Guest

Thank a lot guys,

The idea of building a custom control that will contain all the control
needed to be in the tab is very convenient.
And in run-time, I simply adding a new tab and putting a new instance of the
custom made control on it.


Thank you all
Sharon
 
C

Colby Africa

Thank a lot guys,

The idea of building a custom control that will contain all the control
needed to be in the tab is very convenient.
And in run-time, I simply adding a new tab and putting a new instance of the
custom made control on it.

Thank you all
Sharon

Pete is the man!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top