To copy the layout of a custom control, you need to create a designer.
Refer to the System.Design assembly and write the code below.
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class MyCustomControlDesigner : ParentControlDesigner
{
public MyCustomControlDesigner() { }
public override void Initialize(IComponent component) {
base.Initialize(component);
MyCustomControl control = (MyCustomControl)component;
EnableDesignMode(control.TabControl, "TabControl");
}
}
public class TabPageCollectionEditor : CollectionEditor
{
public TabPageCollectionEditor()
: base(typeof(TabControl.TabPageCollection)) {
}
protected override object CreateInstance(Type itemType) {
object obj = base.CreateInstance(itemType);
TabPage tabPage = obj as TabPage;
tabPage.UseVisualStyleBackColor = true;
return tabPage;
}
}
Inherits the ParentControlDesigner class and enables the designer for the inner TabControl.
TabPageCollectionEditor is required to make UseVisualStyleBackColor = true; when adding a TabPage.
Next, use the designer when designing your custom control.
Note that instead of serializing the Items property, you need to serialize the TabControl's Controls property.
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
[Designer(typeof(MyCustomControlDesigner))]
public class MyCustomControl : ContainerControl
{
private readonly TabControl tabControl;
public MyCustomControl() {
BackColor = Color.LightGray;
tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;
Controls.Add(tabControl);
}
[Browsable(false)]
public TabControl TabControl {
get { return tabControl; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Editor(typeof(TabPageCollectionEditor), typeof(UITypeEditor))]
public TabControl.TabPageCollection Items {
get {
return tabControl.TabPages;
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ControlCollection TabChildren {
get {
return tabControl.Controls;
}
}
}