【问题标题】:Designer Collection Editor, how to know current index?Designer Collection Editor,如何知道当前索引?
【发布时间】:2017-02-14 14:29:14
【问题描述】:

我的控件有一个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?
    }
}

【问题讨论】:

    标签: c# custom-controls


    【解决方案1】:

    由于缺乏示例,我求助于反编译 .NET Framework 并查看 Microsoft 是如何做到的。 Microsoft 的 TabPageCollectionEditor 位于“System.Design.dll”中。

    您可以像这样访问具有集合的控件:

    var control = Context?.Instance as MyTabControl;
    

    那么获取当前索引是一件小事:

    int currentIndex = control.TabPages.Count;
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 2010-11-21
      • 1970-01-01
      • 2023-01-02
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 2021-11-05
      • 2019-09-21
      相关资源
      最近更新 更多