Finding a Component's (ComponentModel.Component) Parent Form

J

JackRazz

I'm working on a .net component (not a control) and need to find out what form the
component is contained in during runtime (DesignMode=False). With a control, there
is a parent property (and FindForm) to do this with, but no such luck with a
component.

I was thinking that I might be able to iterate thru the Container.Components with
something like this with the Site.Name being the components name. This assumes that
a form is added to the container.components.

Dim c As IComponent
For Each c In Me.Container.Components
Console.WriteLine(c.Site.Name)
Next

But this doesn't seem to work. Does anyone have any idea as to how I can determine
what form a component is placed on??

Thanks - JackRazz
 
J

Jorge

Hi Jack

I had the same problem that i was trying to browse all
ContextMenus of a form and there components. I tried the
Site that you did but its not working.

You can use System.Reflexion.FieldInfo() to browse all
controls and components of a windows form.

Kind Regards
Jorge Cavalheiro
-----Original Message-----
I'm working on a .net component (not a control) and need to find out what form the
component is contained in during runtime
(DesignMode=False). With a control, there
is a parent property (and FindForm) to do this with, but no such luck with a
component.

I was thinking that I might be able to iterate thru the Container.Components with
something like this with the Site.Name being the
components name. This assumes that
 
J

JackRazz

Hey Jorge,
I initially thought your idea wouldn't work, but then I realized that form classes
may be static classes. I tested and yep, it is! Any ideas on how to walk thru the
form classes in an assembly and determine which form class a specific instance of a
component (from within that component instance) is on, as every form class would have
my component added on it.

Any ideas on getting a reference to an existing instance of the form or a control
class?

Thanks for the suggestion - JackRazz


| Hi Jack
|
| I had the same problem that i was trying to browse all
| ContextMenus of a form and there components. I tried the
| Site that you did but its not working.
|
| You can use System.Reflexion.FieldInfo() to browse all
| controls and components of a windows form.
|
| Kind Regards
| Jorge Cavalheiro
|
| >-----Original Message-----
| >I'm working on a .net component (not a control) and need
| to find out what form the
| >component is contained in during runtime
| (DesignMode=False). With a control, there
| >is a parent property (and FindForm) to do this with, but
| no such luck with a
| >component.
| >
| >I was thinking that I might be able to iterate thru the
| Container.Components with
| >something like this with the Site.Name being the
| components name. This assumes that
| >a form is added to the container.components.
| >
| >Dim c As IComponent
| >For Each c In Me.Container.Components
| > Console.WriteLine(c.Site.Name)
| >Next
| >
| >But this doesn't seem to work. Does anyone have any
| idea as to how I can determine
| >what form a component is placed on??
| >
| >Thanks - JackRazz
| >
| >
| >
| >
| >
| >
| >.
| >
 
J

John Saunders

JackRazz said:
I'm working on a .net component (not a control) and need to find out what form the
component is contained in during runtime (DesignMode=False). With a control, there
is a parent property (and FindForm) to do this with, but no such luck with a
component.

I was thinking that I might be able to iterate thru the Container.Components with
something like this with the Site.Name being the components name. This assumes that
a form is added to the container.components.

Dim c As IComponent
For Each c In Me.Container.Components
Console.WriteLine(c.Site.Name)
Next

But this doesn't seem to work. Does anyone have any idea as to how I can determine
what form a component is placed on??

What about your components Site property? It has a Container property (as
well as a DesignMode property).
 

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