469,282 Members | 1,947 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

List of Controls on a form

Hi

In a Windows forms application I have 2 forms A and B; Form B inherits from
form A. Form A is never displayed and its only purpose is to be inherited
from and therefore contains mostly public/protected methods that I have
created (as well a few controls that inheriting forms may use). In my method
on form A (public virtual void setcontrols()) I need a list of all controls
on the instantiated form (in this example form B). Form B has a Menu (with 5
MenuItems), an inherited Panel and Button and an additional Button. Help
seems to indicate that "this.Controls.Count" should return an integer count
of all the controls on the instantiated form B. "this.Controls.Count" returns
the actual int value of 1 rather than 8 (which is what I expect).

On the basis that "this.Controls" is the correct method to use, why don't a
get a count (8) of all the controls and is "this.Controls" the correct method
to use anyway? If this is not the correct method then what is the correct
method?

Thanks in advance.
Nov 16 '05 #1
3 3855
OK

Nov 16 '05 #2
On Mon, 21 Mar 2005 03:17:01 -0800, Roger wrote:
Hi

In a Windows forms application I have 2 forms A and B; Form B inherits from
form A. Form A is never displayed and its only purpose is to be inherited
from and therefore contains mostly public/protected methods that I have
created (as well a few controls that inheriting forms may use). In my method
on form A (public virtual void setcontrols()) I need a list of all controls
on the instantiated form (in this example form B). Form B has a Menu (with 5
MenuItems), an inherited Panel and Button and an additional Button. Help
seems to indicate that "this.Controls.Count" should return an integer count
of all the controls on the instantiated form B. "this.Controls.Count" returns
the actual int value of 1 rather than 8 (which is what I expect).

On the basis that "this.Controls" is the correct method to use, why don't a
get a count (8) of all the controls and is "this.Controls" the correct method
to use anyway? If this is not the correct method then what is the correct
method?

Thanks in advance.


Hmm... Do you have a panel (or a group box) as the main control in your
form A? And the other controls are placed inside this panel?

If so, with this.Controls.Count you only get the panel. Because the other
controls are not inside your form, but inside the panel.

So to get the number of controls, I think you have to do something like
this:

int count = this.mypanel.Controls.Count; or
int count = this.mygroupbox.Controls.Count;

But again, if inside your panel are sub panels or group boxes, such a sub
panel again is just counted with 1. The controls inside the subpanel are
not counted. If you need them as well, you have to go recursive thur all
controls of your form and for each control you have to go thru its child
controls and so on.

hth.
--
Claudio Grazioli
http://www.grazioli.ch
Nov 16 '05 #3
Roger,

The controls in your base form are probably declared as private, when that
would be public, you could use them as

base.mycontrol
You will always see them as
base.controls.count

I hope this helps,

Cor
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Kay | last post: by
5 posts views Thread by Vigneshwar Pilli via DotNetMonster.com | last post: by
4 posts views Thread by Toze | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.