C# 设计器集合编辑器,如何知道当前索引?

C# 设计器集合编辑器,如何知道当前索引?,c#,custom-controls,C#,Custom Controls,我的控件有一个子控件集合TabPage类型。我想创建一个新的子实例,根据其索引分配名称。为此,我需要知道集合的当前状态。但是怎么做呢CreateInstance仅提供项目类型,而不提供集合引用 public class MyEditor : CollectionEditor { public MyEditor(Type type) : base(type) { } protected override Type[] CreateNewItemTypes()

我的控件有一个子控件集合
TabPage
类型。我想创建一个新的子实例,根据其索引分配名称。为此,我需要知道集合的当前状态。但是怎么做呢
CreateInstance
仅提供项目类型,而不提供集合引用

public class MyEditor : CollectionEditor
{
    public MyEditor(Type type) : base(type)
    {
    }

    protected override Type[] CreateNewItemTypes()
    {
        return new[]
        {
            typeof(TabPage)
        };
    }

    protected override object CreateInstance(Type itemType)
    {
        return new TabPage("Page 1"); //<-- How to know current index?
    }
}
公共类MyEditor:CollectionEditor
{
公共MyEditor(类型):基(类型)
{
}
受保护的覆盖类型[]CreateNewItemTypes()
{
返回新的[]
{
类型(选项卡页)
};
}
受保护的重写对象CreateInstance(类型itemType)
{

return new TabPage(“第1页”);//由于缺少示例,我求助于对.NET Framework进行反编译,看看Microsoft是如何做到的。Microsoft的TabPageCollectionEditor位于“System.Design.dll”中

您可以访问包含以下集合的控件:

var control = Context?.Instance as MyTabControl;
然后,获取当前索引是一件小事:

int currentIndex = control.TabPages.Count;