Accesing form's components in design time

G

Guest

Hi,

I need to access all components of a winform (controls, and any other
component which does not inherit from System.Windows.Forms.Control) both in
design and run time.

In design time I access the components and iterate through them using form's
Component property, but in run time this property is null. I can access all
the controls using form's Controls property, but I need to access also the
other components such as ContextMenu, ToolTips... which are not Controls.

How can I get this?
Thanks in advance.
 
G

Guest

This is me again,

I wanted to add that in design time I access the Components collection
through form.Container.Components and that in run time accessing this way or
using form.components.Components or form.Components doesn't work.

Thanks again.
 
S

Stoitcho Goutsev \(100\)

Hi,

In design time all the components are kept in a flat collection, by the
container, regardless of the hierarchical structure of the form or control.
The container keep all designable components - control and non-control.

At run time though only control components are stored in a collection and
this is not a flat collection, that is each control has Controls collection
that keep references only to its immediate children. In other words to get
all conotrls you need to traverse the control's tree.

As far as it goes for non-control components in run time you may not find
them in any collection. Usualy they are objects of some classes that your
form keeps reference to in a private fields. Some of the components need
disposal, so they are added to the form's *components* collection, but these
are just few. If you don't take care of adding non-visual componets to some
collection no one will. What I mean in the form constructor after calling
InitializeComponents you need to create a collection and add reference to
each component that you need to access later on.
 
G

Guest

Thanks Stoitcho,

I'm actually doing what you suggested, but what I want is to avoid having to
manually add each component and do it in a loop where no matters how many
components are in the form.

Any ideas?
 
S

Stoitcho Goutsev \(100\)

No, no ideas and I don't think this is possible, though. You can reflect the
form's type looking for fields of types deriving from Component or
implementing IComponent. It is realy weird, though to reflect an object you
know everything about.
 
G

Guest

Yes! I'm on it right now ;)

By reflection I get an array of System.Reflection.FieldInfo with all the
private fields on my class. Some of them are the controls and components I
want to add to my collection, so to filter an eliminate the other ones, I
check if they implement an interface I have defined (I'm getting custom
controls and components).

Now the point is: how can I add the instances of the filtered Fields to my
collection (which is a System.ComponentModel.Container)? I mean, I have
FieldInfo objects, but what I need to add to my collection is the existing
class' instance of that Fields... Do you think it's possible?

Thanks again!
 
S

Stoitcho Goutsev \(100\)

Yes, you call FieldInfo.GetValue method to get the value for the field (the
reference to the component)
 

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