Search code examples
c#winformstabcontroltabpagecollectioneditor

C# TabControl, How create custom TabPages collection editor?


I have customized a TabPage, but found that there are problems as follows:

First, I Create two custom TabPage, It worked.

But there was a problem when I closed the document and then I reopened the document:

enter image description here

Look, TabPage becomes four, but I found the problem in the "designer.cs" document.

// 
// blK_TabControl1
// 
this.blK_TabControl1.Controls.Add(this.blK_TabPage6);
this.blK_TabControl1.Controls.Add(this.blK_TabPage7);
this.blK_TabControl1.Location = new System.Drawing.Point(4, 12);
this.blK_TabControl1.Name = "blK_TabControl1";
this.blK_TabControl1.SelectedIndex = 0;
this.blK_TabControl1.Size = new System.Drawing.Size(604, 196);
this.blK_TabControl1.TabIndex = 14;
this.blK_TabControl1.TabPages.AddRange(new System.Windows.Forms.TabPage[] {
this.blK_TabPage6,
this.blK_TabPage7});

After the normal TabControl add TabPage, there is no "TabPages.AddRange()" this code, how can I fix it?

Here is my code:

 public class BLK_TabPageCollectionEditor : CollectionEditor
 {
        public BLK_TabPageCollectionEditor(Type type)
            : base(type)
        {

        }

        protected override bool CanSelectMultipleInstances()
        {
            return false;
        }


        protected override Type CreateCollectionItemType()
        {
            return typeof(BLK_TabPage);
        }

        protected override object CreateInstance(Type itemType)
        {
            BLK_TabPage tabPage = (BLK_TabPage)itemType.Assembly.CreateInstance(itemType.FullName);

            IDesignerHost host = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            host.Container.Add(tabPage);
            //this.Context.Container.Add(tabPage);

            tabPage.Text = tabPage.Name;
            return tabPage;
        }

 }

public class BLK_TabControl : TabControl
{
    [EditorAttribute(typeof(BLK_TabPageCollectionEditor), typeof(UITypeEditor))]
    [MergableProperty(false)]
    public new TabControl.TabPageCollection TabPages
    {
        get
        {
            return base.TabPages;
        }
    }

}

Thanks in advance.


Solution

  • I have tried your code. Looks like everything is fine. But based on designer-generated code and your image description the only thing I can suggest is to hide serialization of TabPages property:

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [EditorAttribute(typeof(BLK_TabPageCollectionEditor), typeof(UITypeEditor))]
        [MergableProperty(false)]
        public new TabControl.TabPageCollection TabPages
        {
            get
            {
                return base.TabPages;
            }
        }