PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Determining parent form of Component

Reply

Determining parent form of Component

 
Thread Tools Rate Thread
Old 19-10-2005, 11:00 AM   #1
=?Utf-8?B?Um9iZXJ0IE1hZ251c3Nvbg==?=
Guest
 
Posts: n/a
Default Determining parent form of Component


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
  Reply With Quote
Old 19-10-2005, 11:37 AM   #2
Atul
Guest
 
Posts: n/a
Default Re: Determining parent form of Component

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
----------------


"Robert Magnusson" <RobertMagnusson@discussions.microsoft.com> wrote in
message news:2A6FE67D-9EA6-4220-AF21-8E712D0679C8@microsoft.com...
> 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



  Reply With Quote
Old 20-10-2005, 02:35 PM   #3
Nadav P.
Guest
 
Posts: n/a
Default Re: Determining parent form of Component


Atul wrote:
> 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

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off