c#在创建新的tabpages时添加groupbox

问题描述 投票:0回答:1

我刚刚在Visual Studio中使用C#创建了一个应用程序。我想知道是否有可能在用户单击添加新选项卡按钮(+)时创建带有文本框和标签的组框的新实例,如图所示。例如,当用户打开Goog​​le Chrome时,只要用户打开新标签页,它就会在每个页面上显示相同的搜索框。至于现在,我可以在用户单击添加按钮时创建新标签页,但新标签页将为空。

enter image description here

c# winforms tabpage
1个回答
0
投票

你需要使用“using System.Reflection;”

基本上是这样的:

TabPage tpOld = tabControl1.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);
    }
    tpNew.Controls.Add(cNew);
}
tabControl1.TabPages.Add(tpNew);

此代码将复制旧tabPage的所有控件,并将它们放入一个新的标签页,其中包含旧标签的确切控件,位置和大小。

积分在这里:here

© www.soinside.com 2019 - 2024. All rights reserved.