Determining parent form of Component

G

Guest

Hi all,

I have a component (originally developed as a class) that handles the
processing of window docking. When implemented as a class, I simply passed
the form (which implements ContainerControl) into the constructor. I
expected that, in implementing a Component I would be able to determine the
form on which the component was added without having to have it passed in as
a parameter in the constructor (or as a writable property).

Have I missed something (probably!)? How would you go about determining the
form on which a component resides?

Regards,

RM
 
A

Atul

I don't think there is a straight-forward way. One way is to generate
serialization code using CodeDOM for your component similar to the following
:

this.MyComponent1.HostForm = this;

For this, you have to implement your own serialize inheriting from
CodeComSerializer and apply it to your component using
DesignerSerializerAttribute

----------------
-Atul, Sky Software http://www.ssware.com
Shell MegaPack For .Net & ActiveX
Windows Explorer GUI Controls
&
Quick-Launch Like Appbars, MSN/Office2003 Style Popups,
System Tray Icons and Shortcuts/Internet Shortcuts
 
N

Nadav P.

Atul said:
I don't think there is a straight-forward way. One way is to generate
serialization code using CodeDOM for your component similar to the following
:

this.MyComponent1.HostForm = this;

For this, you have to implement your own serialize inheriting from
CodeComSerializer and apply it to your component using
DesignerSerializerAttribute

You don't need to write your own serialization code.
All you have to do is override the Site property.
Then when you drag the component on the form the Code in the Site
property set method finds the parent control and saves it to the
ParentControl property.
The visual designer will then serialize the ParentControl property in
the code.

/// <summary>
/// Gets or sets the System.ComponentModel.ISite" of
/// the Component.
/// used to update ParentControl so it will be serialized to code.
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public override ISite Site {
get { return base.Site; }
set {
base.Site = value;
if(base.Site == null)
return;

// If the component is dropped onto a form during design-time,
// set the ParentControl property.
IDesignerHost host = (value.GetService(typeof(IDesignerHost)) as
IDesignerHost);
if(host != null) {
Control parent = host.RootComponent as Control;
if(parent != null)
ParentControl = parent;
}
}
}

private Control parentControl=null;
/// <summary>
/// Gets or sets the parent Control of the Component.
[Browsable(false)]
public Control ParentControl {
get { return parentControl; }
set {
parentControl = value;
}
}

Hope this Helps,

Nadav
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top