The correct way to override Control.ControlCollection

I started creating my own TabControl widget so that I could accurately draw a tab with closed X on the right edge of the tab. I have my own array class that contains all the tabs.

Therefore, I override the instance class CreateControlsInstance and override the Controls class so that I can hide it during reflection serialization.

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

Then I create an override class.

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

This currently works, but if the Controls class can still call SetChildIndex methods, etc., which modifies the underlying arraylist, but not the tabPages array.

I would like to be able to eliminate the need for the new ControlCollection class to use the base class to register new xTabPage objects with xTabControl.

I already went through the class structure with .Net Reflector. I hope you don’t have to copy half of the Control class to get a new widget registered.

I know this is a long shot, but did anyone succeed?

+4
source share
2 answers

In the course of my research on this issue, I could not find an instance in which UserControls could be managed without using System.Windows.Forms.Control.ControlCollection due to the number of functions provided by the Control function for the Add function. It was even worse when I started to include the Designer in the equation. So I decided to use the Controls property with the custom override that I gave above. Now I need my personal _tabPages settings to sync with the Control Collection, and not vice versa.

+1
source

Well, you can use something like .net Reflector (decplier) to extract the tabcontrol class from .net and edit this class.

0
source

Source: https://habr.com/ru/post/1302092/


All Articles