How do you get parent form with non-visual control/component?

How do you get parent form with non-visual control/component?

Post by Kevin Spen » Sat, 01 Dec 2007 21:19:00


You would have to create a reference to the Form in your Component, and set
it when the Component is added to the Form.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
 
 

How do you get parent form with non-visual control/component?

Post by Jack Jacks » Mon, 03 Dec 2007 02:37:56

This is the main reason I stopped using components for anything that
needs a reference to the form, because I also could not find any easy
way to do this.

I can think of two techniques to find the form, neither is very
satisfactory.

1. Have a public shared property in my application object that gets
set to a reference to the form in my form base class constructor
(before InitializeComponent() is called). When the component
initializes, that property contains a reference to the form being
constructed. This presumes that the component is only created by
dropping it on a form, and never with an explicit New.

2. Add the IDispose interface to the component, as well as a custom
interace. IDispose makes the IDE add a reference to the component in
the form's private 'components' list. After the call to
InitializeComponent(), run through the 'components' list, and for each
item that can be cast to the custom interface, call that interface
passing a reference to the form. The big downside to this method is
that this code has to be put in each derived class, because the
'components' list is private to each derived class.

On Sat, 1 Dec 2007 08:21:03 -0800, Spicy Mikey < XXXX@XXXXX.COM >

 
 
 

How do you get parent form with non-visual control/component?

Post by U3BpY3kgTW » Mon, 03 Dec 2007 06:19:00

Thanks Jack. LOL I agree those are uuugly solutions. I suppose I could
continue to base the control off the UserControl class, but I was happy to
see VS2005 offered a component tray so you could easily create and see a
non-visual control without the need to "hide" it by placing it beyond the
forms coordinates, or use the VISIBLE property.

Could it be there's no clean easy way to obtain a reference to the parent
object for a component? Someone tell me it ain't so!!

--
Maz
 
 
 

How do you get parent form with non-visual control/component?

Post by Jack Jacks » Wed, 05 Dec 2007 08:24:02

The IDE adds:
Private components As System.ComponentModel.IContainer
to the hidden designer part of the form, and for any components that
have a constructor with a System.ComponentModel.IContainer as a
parameter, calls that constructor passing a reference to the form's
IContainer object.

Is that what you are referring to? Is it safe to modify that line to
use a different class? I am always very leery of modifying anything
in the designer part of the form.

Even if that is OK, it still means that you have to remember to modify
each derived instance of a class to change the class and add the form
reference as an argument to the constructor. While a little better
than either of the solutions I had come up with, we were hoping for a
solution that would work in derived form classes without adding code
to each.

On Mon, 3 Dec 2007 16:08:11 -0600, "Sheng Jiang[MVP]"
 
 
 

How do you get parent form with non-visual control/component?

Post by Jack Jacks » Wed, 05 Dec 2007 16:21:22

Yes, I guess that might work. What an effort just to get a reference
to the hosting form, which would be so trivial if it had been designed
in.

On Mon, 3 Dec 2007 20:56:47 -0600, "Sheng Jiang[MVP]"
 
 
 

How do you get parent form with non-visual control/component?

Post by Jack Jacks » Thu, 06 Dec 2007 00:10:57

On second thought, how does that help to get the form reference at
runtime?

On Mon, 3 Dec 2007 20:56:47 -0600, "Sheng Jiang[MVP]"
 
 
 

How do you get parent form with non-visual control/component?

Post by Sheng Jian » Fri, 07 Dec 2007 04:42:50

No, it doesn't. The relation is built in design time and reflected in the
designer generated code.

--
Sheng Jiang
Microsoft MVP in VC++



component.
class's
try