Get all existing object of a form...

G

Guest

I try to find a way to get all the existing objects related to a WinForm...

I tried "this.Controls" and "this.components.Components" but I'm not
reaching all the existing objects...

I need to loop across all objects to verify the content of their "tag"
property and modify their "text" or "caption" property depending of the
object type and "tag" property value.

Thanks.
 
T

Tom Spink

PL said:
I try to find a way to get all the existing objects related to a
WinForm...

I tried "this.Controls" and "this.components.Components" but I'm not
reaching all the existing objects...

I need to loop across all objects to verify the content of their "tag"
property and modify their "text" or "caption" property depending of the
object type and "tag" property value.

Thanks.

Hi PL,

You're probably not getting them all, because some controls can contain
other controls. You need to recurse over all the controls like this:

///
private void RecurseControls ( Control baseControl )
{
foreach ( Control ctrl in baseControl.Controls )
{
// Process your control here.
RecurseControls( ctrl );
}
}
///

Then, to run the recursive algorithm, do this:

///
RecurseControls( this );
///

In the context of your form.
 
G

Guest

Hi Tom,

I'm using exactly the same algorithm and I still don't see all the objects...
I noticed that I have this problem espacially with MenuItem (from MainMenu).
I know that it doesn't have any "tag" property associated to them, but
anyway, I'm not seeing the MenuItem object while looping so...

Thanks
 
T

Tom Spink

PL said:
Hi Tom,

I'm using exactly the same algorithm and I still don't see all the
objects... I noticed that I have this problem espacially with MenuItem
(from MainMenu). I know that it doesn't have any "tag" property associated
to them, but anyway, I'm not seeing the MenuItem object while looping
so...

Thanks

Hi PL,

The reason for this is that MenuItems aren't contained within the Controls
collection of their parent. They are contained within the Items
collection, a property of the MenuItem. You'll have to make a special case
for this, and other controls that implement subcontrols by means of
an 'Items' collection.

The main reason for this is that a MenuItem is not a control, so it doesn't
make sense, to keep it in the Controls collection. Not only that, since a
MenuItem isn't a control, it won't "fit" in the Controls collection,
because it doesn't derive from Control.
 
G

Guest

Thanks for your answers Tom

I would have though that it would have a particular collection allowing us
to view all existing objects in an application... It appears not.
 
G

Guest

I noticed that almost all objects are listed under "this" (I see that at
debug time with QuickWatch), is there a way to access it? For now, I'm only
able to access the form, it would be nice to have access to all the others.

Thanks
 

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