C# TabControl,如何创建自定义 TabPages 集合编辑器?
C# TabControl, How create custom TabPages collection editor?
我自定义了一个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;
}
}
}
提前致谢。
我试过你的代码。看起来一切都很好。但是根据设计师生成的代码和您的图像描述,我唯一可以建议的是隐藏 TabPages
属性:
的序列化
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorAttribute(typeof(BLK_TabPageCollectionEditor), typeof(UITypeEditor))]
[MergableProperty(false)]
public new TabControl.TabPageCollection TabPages
{
get
{
return base.TabPages;
}
}
我自定义了一个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;
}
}
}
提前致谢。
我试过你的代码。看起来一切都很好。但是根据设计师生成的代码和您的图像描述,我唯一可以建议的是隐藏 TabPages
属性:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorAttribute(typeof(BLK_TabPageCollectionEditor), typeof(UITypeEditor))]
[MergableProperty(false)]
public new TabControl.TabPageCollection TabPages
{
get
{
return base.TabPages;
}
}