Copy TabControl Tab

后端 未结 3 423
别那么骄傲
别那么骄傲 2020-12-10 16:21

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

3条回答
  •  囚心锁ツ
    2020-12-10 16:46

    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

提交回复
热议问题