7

I searched the internet for this but i couldn't find how to do it with C#

What i am trying to do is make it so that when i click on my NewTab button, a new tab appears with the same controls that were on the first tab. I saw some information on how to add a UserControl to your form, but C# doesn't have anything like that.

And for everyone who would say "Post your code", i don't have any, so don't bother saying that, the only code i have is the code for the program and that wouldn't help anyone.

Dozer789
  • 1,980
  • 2
  • 23
  • 43
  • 4
    `C# doesn't have a UserControl?` That's an odd statement. Yes, make a UserControl for the contents of your tab, then when you make a new tab page, you instantiate that UserControl and add it to the new TabPage controls collection. Wire up any events accordingly. – LarsTech Jan 24 '13 at 17:32
  • Where do i find the `UserControl`? – Dozer789 Jan 24 '13 at 17:32
  • Off top of my head you'd probably have to add a tab to the tabs collection, then iteratively process each control, create a new instance and copy the properties across using reflection then add to the new tab. Obviously not as simple as this, but might get you started. UserControl - why don't you try adding a new UserControl item to your C# project and have a play with it. – Andez Jan 24 '13 at 17:33
  • Just figured out i had to add it to the code, i always saw how to do it by dragging it from the toolbox. – Dozer789 Jan 24 '13 at 17:34
  • i just added the `UserControl` by code, should i do it a different way? – Dozer789 Jan 24 '13 at 17:34

3 Answers3

9

EDIT

I have rewritten my solution to use reflection.

using System.Reflection;

// your TabControl will be defined in your designer
TabControl tc;
// as will your original TabPage
TabPage tpOld = tc.SelectedTab;

TabPage tpNew = new TabPage();
foreach(Control c in tpOld.Controls)
{
    Control cNew = (Control) Activator.CreateInstance(c.GetType());

    PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(c);

    foreach (PropertyDescriptor entry in pdc)
    {
        object val = entry.GetValue(c);
        entry.SetValue(cNew, val);
    }

    // add control to new TabPage
    tpNew.Controls.Add(cNew);
}

tc.TabPages.Add(tpNew);

Some information can be found here. http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms

Community
  • 1
  • 1
Lorcan O'Neill
  • 3,303
  • 1
  • 25
  • 24
  • and where do i put that code? in the "New Tab" button click, or in the `public Form1()`? – Dozer789 Jan 24 '13 at 17:36
  • 1
    Would just move the controls from one tab to the other? – Andez Jan 24 '13 at 17:38
  • Iterate through the controls of the template TabPage and make clone and then add to the new TabPage; otherwise if you add the control without cloning it will simply move the controls from one TabPage to other. – Manish Jan 24 '13 at 18:05
  • That works, accept that when i do that, the controls on the other tab that i just added are useless, i can't click them and when i change tabs, where the controls were it is just completely white. Any way to fix that? – Dozer789 Jan 25 '13 at 15:09
1

Your best bet would be to look at this article:

Code Project

Then apply the following code to add the cloned control (this would be in your button click handler code (based on article):

    private void button1_Click(object sender, EventArgs e)
    {
        // create new tab
        TabPage tp = new TabPage();

        // iterate through each control and clone it
        foreach (Control c in this.tabControl1.TabPages[0].Controls)
        {
            // clone control (this references the code project download ControlFactory.cs)
            Control ctrl = CtrlCloneTst.ControlFactory.CloneCtrl(c);
            // now add it to the new tab
            tp.Controls.Add(ctrl);
            // set bounds to size and position
            ctrl.SetBounds(c.Bounds.X, c.Bounds.Y, c.Bounds.Width, c.Bounds.Height);
        }

        // now add tab page
        this.tabControl1.TabPages.Add(tp);
    }

Then you would need to hook the event handlers up. Will have to think about this.

Andez
  • 5,588
  • 20
  • 75
  • 116
1

I know it's an old thread but I just figured out a way for myself and thought I should share it. It's really simple and tested in .Net 4.6.

Please note that this solution does not actually create new controls, just re-assigns them all to new TabPage, so you have to use AddRange each time you change tabs. New tab will show the exact same controls, content and values included.

// Create an array and copy controls from first tab to it.
Array tabLayout = new Control [numberOfControls];
YourTabControl.TabPages[0].Controls.CopyTo(tabLayout, 0);

// AddRange each time you change a tab.
YourTabControl.TabPages[newTabIndex].Controls.AddRange((Control[])tabLayout);
Bogna
  • 21
  • 5