I
inpuarg
Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
Windows form without using recursive methods ?
"Every recursive function can be transformed into an iterative function by
using a stack."
So, yes it is possible.
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 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;
}
}
}
Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
Is here not aprivate 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;
}
}
}
Does that not miss the goal of the OP: a complete list of all controls?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.
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 <--
Okay.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.
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.