You do this in order to ensure that your component gets disposed along with the contained Form (logical parent).
All Form derived classes come with an IContainer field into which many of the .NET components like ImageList and Timer add themselves. The Form will dispose the contents of this IContainer from within its Dispose method.
Scenario 1
In order for your Component to get added to this IContainer list, all you have to do is provide a constructor that takes IContainer as its only argument. The design-time will discover this constructor automatically and use it to initialize your component. You should then add yourself to the IContainer in the constructor implementation.
public class CustomComponent : Component
{
public CustomComponent() {}
public CustomComponent( IContainer container )
{
container.Add( this );
}
}
Note: for this to work your Component should not have a custom TypeConverter that can convert your type to an InstanceDescriptor.
Scenario 2
Your components might have more constructors besides the default constructor and you might have a custom TypeConverter that provides an InstanceDescriptor to let your designer use a non-default constructor for initializing your component in code.
In this case, the above approach will not work because you do not have an IContainer-only constructor.
Instead, you have to recreate what the design-time did for you. You have to provide a custom IDesignerSerializationProvider to do so. The attached ContainerInsertingSerializationProvider class can be used to get the above effect.
Contributed from George Shepherd's Windows Forms FAQ