Form.ControlCollection - overrideable?

  • Thread starter Thread starter Phill. W
  • Start date Start date
P

Phill. W

Is it possible to extend the Windows.Forms.Form.ControlCollection
class and override it's Add method?

I'm adding controls to a Form and want to [automatically] hook up
Event Handling for the Form to deal with on behalf of each Control.
I /thought/ the best way to do this would be to inherit from and
override the Add method on the Form.ControlCollection class and
put some AddHandler statements in there. This much I can do but the
bit I'm missing is how to get the Form to use /my/ ControlCollection
rather than the standard one.

Any suggestions?

TIA,
Phill W.
 
Phill. W said:
Is it possible to extend the Windows.Forms.Form.ControlCollection
class and override it's Add method?

I'm adding controls to a Form and want to [automatically] hook up
Event Handling for the Form to deal with on behalf of each Control.
I /thought/ the best way to do this would be to inherit from and
override the Add method on the Form.ControlCollection class and
put some AddHandler statements in there. This much I can do but the
bit I'm missing is how to get the Form to use /my/ ControlCollection
rather than the standard one.

Did you see the form's 'ControlAdded' event?

<URL:http://msdn.microsoft.com/library/e...windowsformscontrolclasscontroladdedtopic.asp>
 
Phill,
In addition to deriving a new class from Control.ControlCollection and
overriding some of its methods. To get Control to use your new class you
need to override Control.CreateControlsInstance to create a new instance of
your ControlCollection.

However!! I would do as Herfried suggested & handle the Control.ControlAdded
& Control.ControlRemoved events.

Note the above are advanced members VB.NET normally hides them in both the
Object Browser & the code editor. Use "Show hidden members" in the object
browser to see them, use "Tools - Options - Text Editor - Basic - General -
Hide advanced members" to see them in the code editor.

Hope this helps
Jay
 
Herfried K. Wagner said:
Phill. W said:
Is it possible to extend the Windows.Forms.Form.ControlCollection
class and override it's Add method?

I'm adding controls to a Form and want to [automatically] hook up
Event Handling for the Form to deal with on behalf of each Control.
I /thought/ the best way to do this would be to inherit from and
override the Add method on the Form.ControlCollection class and
put some AddHandler statements in there. This much I can do but the
bit I'm missing is how to get the Form to use /my/ ControlCollection
rather than the standard one.

Did you see the form's 'ControlAdded' event?
<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsfo
rmscontrolclasscontroladdedtopic.asp>

Many thanks, Herfried; just what the Doctor ordered ...

And, of course, thanks once again to Our Friends in Redmond's
and their utterly /impenetrable/ documentation. ;-)

Regards,
Phill W.
 
Back
Top