Access form from component

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello.

I have created a custom component. At run-time I need to access the Form
that is hosting my component from inside my component. Is this possible?

Thanks in advance.
 
That was a tricky one, no wonder that nobody answered.

First, I added a property named ParentForm to my component:

public Form ParentForm
{
get
{ return m_oParentForm; }

set
{ m_oParentForm = value; }
}

Then, I overrided the IComponent.ISite property in my component and in the
setter part I added the following code:

public override ISite Site
{
get
{ ...}

set
{
IDesignerHost h = (IDesignerHost
)value.Services.GetService(typeof(IDesignerHost) );
this.ParentForm = (Form)h.RootComponent;
}
}

this forces the windows forms designer to add the following
line to the components initialization code:

this.MyComponent.ParentForm = this;
 
Back
Top