Designers for System.ComponentModel.Component do not always Add to parent Container

  • Thread starter Thread starter Mark Cranness
  • Start date Start date
M

Mark Cranness

Some designable Components (objects of type
System.ComponentModel.Component) when dropped onto a Component class
create code to add themselves to the parent component Container and
some do not.

This behaviour seems inconsistent.

Components that do *not* Add themselves to the parent class Container
are not Disposed of automatically when parent.Dispose() is called,
and must be Disposed in some other way.
If all components Added themselves to the parent Container, then all
could be Disposed by calling parent.Dispose()

Example:
- Create a Class Library project and add a Component Class
(Component1.cs) to it.
- From the toolbar, drag and drop a Windows Forms.Timer object
- From the toolbar, drag and drop a Data SqlCommand

Note the following code created:
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
}

Note that timer1 is added to the "components" Container, but
SqlConnection1 is not added.

When the parent component is Disposed, timer1 *will* be also Disposed,
but SqlConnection1 will *not* be automatically disposed.

If the Visual Studio designer also added the following code:

this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.components.Add(this.sqlConnection1);

.... then, with this extra code, when the parent is Disposed, the
SqlConnection will also be disposed, which would be very desirable
automatic behaviour.
(Or use an overriden New() method that takes a Container argument)

Other newsgroup articles that seem to raise this question/problem are:
http://groups.google.com/[email protected]
http://groups.google.com/groups?threadm=u#JwGeHnCHA.1828@TK2MSFTNGP08

Please change Visual Studio .NET to behave in the manner suggested above.
(Or please explain why it should be the way it currently is... :-)

Thanks,
Mark Cranness
 
Back
Top