All Controls On Form Without Using Recursion

I

inpuarg

Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
 
L

Laura T.

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

inpuarg

would you please give me an example ?

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

inpuarg

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 ?
 
D

DeveloperX

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 ?






- 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;
}
}
}
 
D

DeveloperX

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;
}
}
}





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 ?
- 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;
}
}

}
 
O

Oliver Sturm

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
 
S

Stefan Hoffmann

hi,
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 <--
 
D

DeveloperX

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 :)
 
D

DeveloperX

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 :)
 
S

Stefan Hoffmann

hi,
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 <--
 
D

DeveloperX

hi,


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.
 
S

Stefan Hoffmann

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 <--
 

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