简体   繁体   English

正确的方法来覆盖Control.ControlCollection

[英]Proper way to override Control.ControlCollection

I went about creating a custom TabControl widget so that I could accurately paint the tab with a close X on the right edge of the tab. 我开始创建一个自定义TabControl小部件,以便我可以准确地在选项卡的右边缘绘制一个关闭X的选项卡。 I have a custom array class that holds all of the tabs. 我有一个包含所有选项卡的自定义数组类。

So I override the CreateControlsInstance instance class and redefine the Controls class so I can hide it during reflection serialization. 所以我重写CreateControlsInstance实例类并重新定义Controls类,以便我可以在反射序列化期间隐藏它。

protected override Control.ControlCollection CreateControlsInstance() {
  return new ControlCollection( this );
}

[Browsable( false ), DesignerSerializationVisibility( DesignerSerializationVisibility.Hidden )]
private new Control.ControlCollection Controls {
  get { return base.Controls; }
}

I then create the override class. 然后我创建了override类。

public new class ControlCollection: Control.ControlCollection {
  private xTabControl owner;

  public ControlCollection( xTabControl owner ): base( owner ) {
    this.owner = owner;
  }

  public override void Add( Control value ) {
    if ( !(value is xTabPage) )
      throw new Exception( "The control must be of type xTabPage" );

    xTabPage tabPage = (xTabPage)value;

    if ( !owner.inTabEvent )
      owner._tabPages.Add( tabPage );

    base.Add( value );
  }

  public override void Remove( Control value ) {
    if ( !(value is xTabPage) )
      throw new Exception( "The control must be of type JDMX.Widget.xTabPage" );

    if ( !owner.inTabEvent ) {
      xTabPage tabPage = (xTabPage)value;
      owner._tabPages.Remove( tabPage );
    }

    base.Remove( value );
  }

  public override void Clear() {
    owner._tabPages.Clear();
  }
}

Currently this works but if the Controls class can still call methods SetChildIndex, etc which changes the underlying arraylist but not the tabPages array. 目前这是有效的,但如果Controls类仍然可以调用方法SetChildIndex等,它改变了底层的arraylist而不是tabPages数组。

I would like to be able to eliminate the need for the new ControlCollection class to have to use the base class for registering the new xTabPage objects with the xTabControl. 我希望能够消除新的ControlCollection类必须使用基类来使用xTabControl注册新的xTabPage对象。

I have already been through the class structure with .Net Reflector. 我已经通过.Net Reflector完成了类结构。 I am hoping to not have to copy half of the Control class in order to get the registration of the new widget to work. 我希望不必复制Control类的一半,以便使新小部件的注册工作。

I know this is a long shot but has anyone had any success doing this? 我知道这是一个很长的镜头但是有没有人做过这样的成功?

Throughout my research on this, I could not find an instance where UserControls could be managed without using System.Windows.Forms.Control.ControlCollection because of the number of functions the assignment of a Control to the Add function provides. 在我对此研究的整个过程中,我找不到一个可以在不使用System.Windows.Forms.Control.ControlCollection的情况下管理UserControls的实例,因为控件分配给Add函数提供了多少个函数。 It was even worse when I began to incorporate the Designer into the equation. 当我开始将Designer纳入等式时,情况更糟。 So I have decided to embrace the Controls property using the custom override I gave above. 所以我决定使用我上面给出的自定义覆盖来包含Controls属性。 I will now need to keep my private level _tabPages synced with the Control Collection and not the other way around. 我现在需要保持我的私人级别_tabPages与Control Collection同步,而不是相反。

那么你可以使用.net Reflector(decomplier)之类的东西从.net中提取tabcontrol类并编辑该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM