473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

All Controls On Form Without Using Recursion

Is it possible to get all controls and all of their children on a
Windows form without using recursive methods ?
Feb 1 '07 #1
13 12953
"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
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
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
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
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
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
Thank you - it works like a charm.
On 1 Feb 2007 11:41:27 -0800, "DeveloperX" <nn*****@operamail.com>
wrote:
Feb 2 '07 #8
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: DraguVaso | last post by:
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...
4
by: David A. Beck | last post by:
Is there a way I can loop through a form object in ASP.NET codebehind like in VB.NET and get all of the controls?
4
by: Milsnips | last post by:
hi there, i have a strange problem. I want to programatically loop through each control on a page, but am having issues. my test example: 1. header.ascx - the ascx control finds all the...
11
by: Frank Esser | last post by:
Hi, I created an ASP.NET page (test.aspx) with some web controls in GridLayout (Design time). When I look at the collection page.controls by foreach (Control ctrl in Page.Controls) { ....
7
by: Rodusa | last post by:
I am trying to loop all controls in a form with no success. For example, Let's say we place three TextBoxes in the form and we use the code below: foreach (Control c in Page.Controls) {...
3
by: J | last post by:
I'm trying to make a list of certain properties of all the controls on an ASP.NET page. Does anyone know of an easy way to do this? I've tried to do it a couple of different ways but I'm not...
7
by: astro | last post by:
I am not farmilar with the object model for webforms. I want to loop through the web form controls - pulling out the checkboxes on the form like the following: For Each ctrl In Me.Controls ...
19
by: ThatsIT.net.au | last post by:
I come from a classic asp background, but have started using ASP.NET about 12 months ago, but I'm still not sure about the pros and cons of using controls v HTML spat out from code as you would in...
2
by: drewsdepaul | last post by:
I have a asp.net web form running in the 2.0 framework. The page has a mixture of asp.net controls and older controls (ex: <input type="radio" ) When the pages posts back to itself, all of...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.