List of Controls on a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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.
 
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
 
Back
Top