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

All Controls On Form Without Using Recursion

P: n/a
Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
Feb 1 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"Every recursive function can be transformed into an iterative function by
using a stack."
So, yes it is possible.

"inpuarg" <in*****@whereland.comha scritto nel messaggio
news:a2********************************@4ax.com...
Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?

Feb 1 '07 #2

P: n/a
would you please give me an example ?

On Thu, 1 Feb 2007 11:24:33 +0100, "Laura T." <LT@NOWHERE.COMwrote:
>"Every recursive function can be transformed into an iterative function by
using a stack."
So, yes it is possible.

"inpuarg" <in*****@whereland.comha scritto nel messaggio
news:a2********************************@4ax.com.. .
>Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
Feb 1 '07 #3

P: n/a
private void ResetAllControlsBackColor(Control control)
{
control.BackColor = SystemColors.Control;
if(this.HasChildren)
{
// Recursively call this method for each child control.
foreach(Control childControl in control.Controls)
{
ResetAllControlsBackColor(childControl);
}

this one - for example ?
On Thu, 1 Feb 2007 11:24:33 +0100, "Laura T." <LT@NOWHERE.COMwrote:
>"Every recursive function can be transformed into an iterative function by
using a stack."
So, yes it is possible.

"inpuarg" <in*****@whereland.comha scritto nel messaggio
news:a2********************************@4ax.com.. .
>Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
Feb 1 '07 #4

P: n/a
On 1 Feb, 11:12, inpuarg <inpu...@whereland.comwrote:
private void ResetAllControlsBackColor(Control control)
{
control.BackColor = SystemColors.Control;
if(this.HasChildren)
{
// Recursively call this method for each child control.
foreach(Control childControl in control.Controls)
{
ResetAllControlsBackColor(childControl);

}

this one - for example ?

On Thu, 1 Feb 2007 11:24:33 +0100, "Laura T." <L...@NOWHERE.COMwrote:
"Every recursive function can be transformed into an iterative function by
using a stack."
So, yes it is possible.
"inpuarg" <inpu...@whereland.comha scritto nel messaggio
news:a2********************************@4ax.com...
Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?- Hide quoted text -

- Show quoted text -
private void button2_Click(object sender, System.EventArgs e)
{
System.Collections.ArrayList toDo = new ArrayList();
bool working = true;
int index =0;
Control current = null;

foreach(Control c in this.Controls)
{
toDo.Add(c);
}
while (working)
{
current = (Control)toDo[index];
foreach(Control c2 in current.Controls)
{
toDo.Add(c2);
}
current.BackColor = System.Drawing.Color.AliceBlue;
index++;
if(toDo.Count == index)
{
working = false;
}
}
}

Feb 1 '07 #5

P: n/a
I did a stack version too as that's what Laura suggested. The
ArrayList version is great if your controls don't change in number
because you can cache it then just iterate through each time you want
to apply a setting to everything. The Stack version uses less memory,
but won't be quite as quick if you need to run it twice on the same
set of controls.
Google have fixed what ever broke as well. Groups is now working
properly in Opera again :D
private void button2_Click(object sender, EventArgs e)
{
System.Collections.Stack toDo = new Stack();
bool working = true;
Control current = null;

foreach (Control c in this.Controls)
{
toDo.Push(c);
}
while (working)
{
current = (Control)toDo.Pop();
foreach (Control c2 in current.Controls)
{
toDo.Push(c2);
}
current.BackColor = System.Drawing.Color.AliceBlue;
if (0 == toDo.Count)
{
working = false;
}
}
}

On 1 Feb, 11:47, "DeveloperX" <nntp...@operamail.comwrote:
On 1 Feb, 11:12, inpuarg <inpu...@whereland.comwrote:
private void ResetAllControlsBackColor(Control control)
{
control.BackColor = SystemColors.Control;
if(this.HasChildren)
{
// Recursively call this method for each child control.
foreach(Control childControl in control.Controls)
{
ResetAllControlsBackColor(childControl);
}
this one - for example ?
On Thu, 1 Feb 2007 11:24:33 +0100, "Laura T." <L...@NOWHERE.COMwrote:
>"Every recursive function can be transformed into an iterative function by
>using a stack."
>So, yes it is possible.
>"inpuarg" <inpu...@whereland.comha scritto nel messaggio
>news:a2********************************@4ax.com.. .
>Is it possible to get all controls and all of their children on a
>Windows form without using recursive methods ?- Hide quoted text -
- Show quoted text -

private void button2_Click(object sender, System.EventArgs e)
{
System.Collections.ArrayList toDo = new ArrayList();
bool working = true;
int index =0;
Control current = null;

foreach(Control c in this.Controls)
{
toDo.Add(c);
}
while (working)
{
current = (Control)toDo[index];
foreach(Control c2 in current.Controls)
{
toDo.Add(c2);
}
current.BackColor = System.Drawing.Color.AliceBlue;
index++;
if(toDo.Count == index)
{
working = false;
}
}

}

Feb 1 '07 #6

P: n/a
Hello inpuarg,
>Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
Why would you want to do that? Any reason apart from theory? As the
controls are stored in a hierarchical structure, recursion seems to be a
very natural way of enumerating them.
Oliver Sturm
--
http://www.sturmnet.org/blog
Feb 1 '07 #7

P: n/a
Thank you - it works like a charm.
On 1 Feb 2007 11:41:27 -0800, "DeveloperX" <nn*****@operamail.com>
wrote:
Feb 2 '07 #8

P: n/a
hi,

DeveloperX wrote:
private void button2_Click(object sender, EventArgs e)
{
System.Collections.Stack toDo = new Stack();
bool working = true;
Control current = null;

foreach (Control c in this.Controls)
{
toDo.Push(c);
}
while (working)
{
current = (Control)toDo.Pop();
Is here not a
toDo.Push(current);
missing?
foreach (Control c2 in current.Controls)
{
toDo.Push(c2);
}
current.BackColor = System.Drawing.Color.AliceBlue;
if (0 == toDo.Count)
{
working = false;
}
}
}

mfG
--stefan <--

Feb 2 '07 #9

P: n/a
Oopsie indeed. I know how I missed it as well. I created the test
harness I used at work, with a couple of buttons, a panel and a button
on the panel, and cleverly dropped the button over the panel instead
of in it, so everything went blue :)
Well spotted, thanks for the correction :)
On 2 Feb, 12:41, Stefan Hoffmann <stefan.hoffm...@explido.dewrote:
hi,

DeveloperX wrote:
private void button2_Click(object sender, EventArgs e)
{
System.Collections.Stack toDo = new Stack();
bool working = true;
Control current = null;
foreach (Control c in this.Controls)
{
toDo.Push(c);
}
while (working)
{
current = (Control)toDo.Pop();

Is here not a
toDo.Push(current);
missing?
foreach (Control c2 in current.Controls)
{
toDo.Push(c2);
}
current.BackColor = System.Drawing.Color.AliceBlue;
if (0 == toDo.Count)
{
working = false;
}
}
}

mfG
--stefan <--

Feb 2 '07 #10

P: n/a
Right checked it again, and it does actually work, although I had
dropped the button over the panel, but it works with it in the panel
too. We don't want a push where you've suggested because that would
put the current control back on the stack, and once we've finished
with it we want it off the stack so we can forget about it.

So the idea is we load all the top level controls into the stack at
the start, then remove them one at a time and push all it's child
controls onto the stack.

Thanks for looking though :)

On 2 Feb, 16:30, "DeveloperX" <nntp...@operamail.comwrote:
Oopsie indeed. I know how I missed it as well. I created the test
harness I used at work, with a couple of buttons, a panel and a button
on the panel, and cleverly dropped the button over the panel instead
of in it, so everything went blue :)
Well spotted, thanks for the correction :)

On 2 Feb, 12:41, Stefan Hoffmann <stefan.hoffm...@explido.dewrote:
hi,
DeveloperX wrote:
private void button2_Click(object sender, EventArgs e)
{
System.Collections.Stack toDo = new Stack();
bool working = true;
Control current = null;
foreach (Control c in this.Controls)
{
toDo.Push(c);
}
while (working)
{
current = (Control)toDo.Pop();
Is here not a
toDo.Push(current);
missing?
foreach (Control c2 in current.Controls)
{
toDo.Push(c2);
}
current.BackColor = System.Drawing.Color.AliceBlue;
if (0 == toDo.Count)
{
working = false;
}
}
}
mfG
--stefan <--

Feb 2 '07 #11

P: n/a
hi,

DeveloperX wrote:
So the idea is we load all the top level controls into the stack at
the start, then remove them one at a time and push all it's child
controls onto the stack.
Does that not miss the goal of the OP: a complete list of all controls?

I would assume that complete means also the parent controls.
mfG
--stefan <--
Feb 5 '07 #12

P: n/a
On 5 Feb, 10:11, Stefan Hoffmann <stefan.hoffm...@explido.dewrote:
hi,

DeveloperX wrote:
So the idea is we load all the top level controls into the stack at
the start, then remove them one at a time and push all it's child
controls onto the stack.

Does that not miss the goal of the OP: a complete list of all controls?

I would assume that complete means also the parent controls.

mfG
--stefan <--
Well I provided the two listings for that reason. The ArrayList
version will generate a full list of all controls, the stack version
doesn't prove a cache, but is memory efficient. That's basically the
difference between the two.
The Stack version will end up with an empty stack, that's how we know
it has finished processing. The main reason people don't want to use
recursion is where the depth is significant, and as Laura T says,
every Recursive solution can be replaced with an iterative one.

Feb 5 '07 #13

P: n/a
hi,

DeveloperX worte:
The Stack version will end up with an empty stack, that's how we know
it has finished processing. The main reason people don't want to use
recursion is where the depth is significant, and as Laura T says,
every Recursive solution can be replaced with an iterative one.
Okay.
mfG
--stefan <--
Feb 6 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.