Live Chat Icon For mobile
Live Chat Icon

How to make my Component add itself to the contained Form’s IContainer list?

Platform: WinForms| Category: Serialization

You do this inorder 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 to. The Form will dispose the contents of this IContainer from within its Dispose.

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 the one and 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.

Note that for this to work your Component should not have a custom TypeConverter that can convert your type to an InstanceDescriptor.

Example:


public class MyComponent : Component
{
  public MyComponent()
  {
  }

  public MyComponent(IContainer container)
  {
    container.Add(this);
  }
}

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-argument only constructor.

You now 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.

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.