you're reading...

Notifiying a Component about its Owner

What’s this? A post about programming?

Yes, its true; this is mainly in response to a question posted on the DOTNET-WINFORMS mailing list.

I’m writing a component that derives from Component and sits on a forms design surface. At runtime I would like to be able to get hold of the form instance and do stuff to it.

Can I do this by some magical call or by overriding some interface? I’ve tried overriding the Component.ISite get/set but that isn’t being called automatically, at least my breakpoint is never hit.

Read more to see the code, an explanation, and where to find a working example.

An example of this is fairly easy to find, drag a Timer object onto a Form and look for its SynchronizingObject property. You will notice that it has already been populated with the name of your Form. This is possible because of the rich design time environment provided by the .NET framework controls. Included with that is an interface that provides one property we can use, IDesignerHost.RootComponent.

This property will return the document that is

#region Host

private Form host = null;

/// <summary>
///    <para>Gets or sets the <see cref="Form"/> that the component is 
///    attached to.</para>
///    <para>This property must be set for the MDI replacement
///    functionality to work.  If it isn't set and a menu item with
///    the MdiList property set to true is found an exception will be 
///    thrown during the <see cref="ISupportInitialize.EndInit()"/> 
///    method.</para>
/// </summary>
/// <remarks>
///    This prperty can only be changed before EndInit has been called.
///    Changing the host at run-time just isn't worth it.
/// </remarks>
  Description("A reference to the form which contains this component.")
public Form Host
    if( host == null && DesignMode )
      IDesignerHost designerHost = (IDesignerHost) GetService(typeof(IDesignerHost));

      if( designerHost != null && designerHost.RootComponent is Form )
        host = (Form) designerHost.RootComponent;

    return host;
    if( this.host != value )
      this.host = value;

#endregion // Host

I apologize if something is incoherent, I whipped this one up real quick one night then saved it…now i need it to test my feed


About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.


One thought on “Notifiying a Component about its Owner

  1. But using this code, the component can’t be serialized. I need that the component become serialized (for save and continue more later).

    How I can do this?


    Posted by César F. | August 18, 2005, 11:47 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: