Find a compont's parent

  • Thread starter Thread starter Perry van Kuppeveld
  • Start date Start date
P

Perry van Kuppeveld

Hi,

Im writing a component to store size and position of a window. Since there's
no user input I derived it from Component.

The problem now is:
- When the component is dropped I need to know where it is dropped (design
time). It should only be dropped on a form.
- How do I get a reference to the parent form?

Thank's

Perry
 
Perry I have been suffering the same thing! There is no way to get the windows form from the actual component which seems weird but does seem to be true

I have settled on an alternative of having a member variable in which to store a reference to the parent form. Sounds messy but using the design time stuff you can get it to fill the variable when the component initialises. Thats what I did, if you find a better way let me know but I've spent some time looking now..... and this comes a pretty close second

this is the code I used in my constructor. (form is a private variable) It basically looks through the design time container for a Form object and makes my internal value match that, thus I have the form to use

if(this.DesignMode && form==null)
foreach(IComponent comp in this.Site.Container.Components)
if(comp.GetType() == typeof(System.Windows.Forms.Form))
form=(System.Windows.Forms.Form)comp




for now it seems to work but I'm pretty new to C# so if you or anyone sees any issues (I can see one if somehow there is more than one winform in the underlying Container but I'm choosing to ostrich this in favour of achiving something!!!) please let me know... or if anyone knows how to do something cleverer with Attributes maybe

Trevor
 
* "Perry van Kuppeveld said:
Im writing a component to store size and position of a window. Since there's
no user input I derived it from Component.

The problem now is:
- When the component is dropped I need to know where it is dropped (design
time). It should only be dropped on a form.
- How do I get a reference to the parent form?

Maybe this article will help you to understand some of the component's
properties:

<http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/>
 
Back
Top