473,320 Members | 2,006 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,320 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 12948
"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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.