C#TabControl,如何创建自定义TabPages集合编辑器?

时间:2017-08-15 02:59:36

标签: c# winforms tabcontrol tabpage collectioneditor

我已经定制了一个TabPage,但发现存在以下问题:

首先,我创建两个自定义TabPage,它工作。

但是当我关闭文档然后我重新打开文档时出现问题:

enter image description here

看,TabPage变为四,但我在“designer.cs”文档中找到了问题。

// 
// 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});

正常的TabControl添加TabPage后,没有“TabPages.AddRange()”这段代码,我该如何解决?

这是我的代码:

 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;
        }
    }

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

我已经尝试过您的代码了。看起来一切都很好。但基于设计器生成的代码和图像描述,我唯一可以建议的是隐藏TabPages属性的序列化:

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