Copy TabControl Tab

I searched the Internet for this, but I could not find how to do this using C #

What I'm trying to do is make sure that when you click the button NewTaba new tab appears with the same controls that were on the first tab. I saw some information on how to add UserControlto your form, but C # has nothing of the kind.

And for everyone who says “Publish your code,” I don’t have one, so don’t worry, saying that the only code that I have is the program code, and this will not help anyone.

+5
source share
4 answers

EDIT

I rewrote my solution for using 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);

. http://www.codeproject.com/Articles/12976/How-to-Clone-Serialize-Copy-Paste-a-Windows-Forms

+8

:

, ( ( ):

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

. .

+1

, , , . .Net 4.6.

Note that this solution does not actually create new controls, but simply reinstalls them all on a new TabPage tab, so you need to use AddRange every time you change the tabs. A new tab displays the same controls, content, and values.

// 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);
0
source

I have a problem copying controls from Tab1 to Tab 2. I don’t know why all the controls were removed from Tab1 to Tab 2 ... Tab1 remained clean.

Dim title As String = "Objednávka " & (TabContrObjednavka.TabCount + 1).ToString()
    Dim myTabPage As TabPage = New TabPage(title)
    TabContrObjednavka.TabPages.Add(myTabPage)

    Dim tabLayout As Array = New Control(TabContrObjednavka.TabPages(0).Controls.Count - 1) {}
    TabContrObjednavka.TabPages(0).Controls.CopyTo(tabLayout, 0)
    TabContrObjednavka.TabPages(TabContrObjednavka.TabCount - 1).Controls.AddRange(CType(tabLayout, Control()))
0
source

All Articles