PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Determining parent form of Component
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Determining parent form of Component
![]() |
Determining parent form of Component |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

