我在网上搜索这个,但找不到如何用c#
我要做的是,当我单击NewTab按钮时,会出现一个新的选项卡,其中包含与第一个选项卡上相同的控件。我看到一些关于如何在表单中添加aUserControl的信息,但是c没有这样的信息。
对于每个说“发布你的代码”的人,我没有任何代码,所以不用费心这么说,我只有程序的代码,这对任何人都没有帮助。

最佳答案

编辑
我重写了使用反射的解决方案。

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

09-13 11:14
查看更多