By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Get ALL controls on a Form

P: n/a
Hi,

Is there actually a way to get ALL the Controls on a Form? While using the
ControlCollection, it only returns the Controls that are directly on the
Form, not the controls that are on a (TableLayout)Panel etc.

I never found something like that, but it just would be nice in my opinion
:-)

Thanks,

Pieter
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi,
you can use following code snippet.

foreach (Control ctr in this.Controls)
{
if (ctr.HasChildren) // Check for Containder Control
{
//Controls on other control (e.g Panel)
}
}

Hope this will solve your problem

Prasad.
"DraguVaso" wrote:
Hi,

Is there actually a way to get ALL the Controls on a Form? While using the
ControlCollection, it only returns the Controls that are directly on the
Form, not the controls that are on a (TableLayout)Panel etc.

I never found something like that, but it just would be nice in my opinion
:-)

Thanks,

Pieter

Nov 21 '05 #2

P: n/a
private ControlCollection res = new ControlCollection();
public void GetControls(Control parent)
{
if (! parent is Form)
{
res.Add(parent);
}

foreach(Control c in parent.Controls)
{
GetControls(c);
}
}

"DraguVaso" <pi**********@hotmail.com> wrote in message
news:ud**************@TK2MSFTNGP10.phx.gbl...
Hi,

Is there actually a way to get ALL the Controls on a Form? While using the
ControlCollection, it only returns the Controls that are directly on the
Form, not the controls that are on a (TableLayout)Panel etc.

I never found something like that, but it just would be nice in my opinion
:-)

Thanks,

Pieter

Nov 21 '05 #3

P: n/a
On 20/09/2005 DraguVaso wrote:
Hi,

Is there actually a way to get ALL the Controls on a Form? While
using the ControlCollection, it only returns the Controls that are
directly on the Form, not the controls that are on a
(TableLayout)Panel etc.

You could use a recursive procedure.

private sub GetControls(Control ctlParent, ref ArrayList al)
{
al.Add(ctlParent);
foreach(Control ctlChild in ctlParent)
GetControls(ctlChild, ref al);
}

Not tested, my development PC is off at the moment but something like
that should do it. Call it by setting up a new ArrayList and starting
with the Windows Form. If it doesn't recognise the Form as a control
you would need to start with:

private sub GetAllControls()
{
ArrayList al = new ArrayList();
foreach(Control ctl in frmMain)
GetControls(ctl, ref al);
}
--
Jeff Gaines
Nov 21 '05 #4

P: n/a
Pieter
Is there actually a way to get ALL the Controls on a Form? While using the
ControlCollection, it only returns the Controls that are directly on the
Form, not the controls that are on a (TableLayout)Panel etc.

Roughly done
\\\
Showall(me)
Private Sub ShowAll(ByVal parentCtr As Control)
For Each ctr As Control In parentCtr.Controls
Console.Write(ctr.name)
ShowAll(ctr)
Next
End Sub
///
I hope this helps,

Cor
Nov 21 '05 #5

P: n/a
Thanks guys, I guess recursion will indeed be the only solution for this. I
think it's kind of weird there isn't a Collection that returns everything on
a form, but so be it, hehe :-)

Pieter

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:eA*************@TK2MSFTNGP10.phx.gbl...
Pieter
Is there actually a way to get ALL the Controls on a Form? While using the ControlCollection, it only returns the Controls that are directly on the
Form, not the controls that are on a (TableLayout)Panel etc.

Roughly done
\\\
Showall(me)
Private Sub ShowAll(ByVal parentCtr As Control)
For Each ctr As Control In parentCtr.Controls
Console.Write(ctr.name)
ShowAll(ctr)
Next
End Sub
///
I hope this helps,

Cor

Nov 21 '05 #6

P: n/a
"DraguVaso" <pi**********@hotmail.com> schrieb:
Is there actually a way to get ALL the Controls on a Form? While using the
ControlCollection, it only returns the Controls that are directly on the
Form, not the controls that are on a (TableLayout)Panel etc.


\\\
Private Sub RecurseControls(ByVal ctr As Control)
Debug.WriteLine(ctr.Name)
If ctr.HasChildren Then
For Each c As Control In ctr.Controls
RecurseControls(c)
Next c
End If
End Sub
..
..
..
RecurseControls(Me)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.