473,326 Members | 2,182 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,326 software developers and data experts.

A real simple one

Hi,

My documetation is deficient on this.

What is the easiest way to iteraterate over all the controls within a form
control using the foreach statement.

Thanx,
Bill
Nov 16 '05 #1
3 1163
For fun. I made a form with a bunch of controls and tried this code in a
button on the form:

string str="";
foreach(Control tb in this.Controls) // GOOD
{
str+=str+tb.Name+"\n";
}
MessageBox.Show(str);
}

It worked but controls names are repeated several times when I expected each
one to appear only once.
Nov 16 '05 #2
web1110 ha scritto:
For fun. I made a form with a bunch of controls and tried this code in a
button on the form:

string str="";
foreach(Control tb in this.Controls) // GOOD
{
str+=str+tb.Name+"\n";
}
MessageBox.Show(str);
}

It worked but controls names are repeated several times when I expected each
one to appear only once.


you must use:

str+=tb.Name+"\n";

or

str=str+tb.Name+"\n";


--
Free Download --> "AB...C#, Guida alla programmazione"
Il manuale in italiano per imparare c#, scaricalo da www.dotnetarchitects.it
email: na***************@qbgargnepuvgrpgf.vg (ROT13-encrypted)
Nov 16 '05 #3
Well, maybe it was worth asking. The code you have (once corrected) will
display names of all the controls in the form's Controls collection, but it
will not necessarily get all the controls on a form. If you have a container
control (such as Panel), then any controls on the Panel will not be in the
form's Controls collection, it will be in the Panel's collection. To really
catch them all, you'd need something like this:

private void Write_Controls_to_output(Control ctrl)
{
// For debugging...
// Writes a line to output for each Control on the form

Console.WriteLine(ctrl.Name + " " + ctrl.GetType().ToString());

if ( ctrl.Controls != null && ctrl.Controls.count > 0 )
foreach( Control c in ctrl.Controls )
{
Write_Controls_to_output(c);
}
}

foreach( Control c in this.Controls)
Write_Controls_to_output(c);
-Rachel

"web1110" <we***@comcast.net> wrote in message
news:Ds********************@comcast.com...
Arggggggghhhhhhhhhh!!!
Thank you. One of those things where you cannot see the forest for the
trees. It was late and I guess I should have quit. Sorry for wasting
everyones time.

"Antonio Pelleriti" <na***************@qbgargnepuvgrpgf.vg> wrote in message news:11**************@tiglio.venaria.marelli.it...
web1110 ha scritto:
For fun. I made a form with a bunch of controls and tried this code in a button on the form:

string str="";
foreach(Control tb in this.Controls) // GOOD
{
str+=str+tb.Name+"\n";
}
MessageBox.Show(str);
}

It worked but controls names are repeated several times when I
expected
each one to appear only once.


you must use:

str+=tb.Name+"\n";

or

str=str+tb.Name+"\n";


--
Free Download --> "AB...C#, Guida alla programmazione"
Il manuale in italiano per imparare c#, scaricalo da

www.dotnetarchitects.it
email: na***************@qbgargnepuvgrpgf.vg (ROT13-encrypted)


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gérard Talbot | last post by:
Hello all, When webfonts are used for purely cosmetic/ornemental reasons and on a large scale, I don't agree. When webfonts are used because Unicode support among browsers for a particular...
23
by: Adam | last post by:
I am coding a microkernel based off of Tanebaum's theroy. For Isis to be extensible, fast, and secure, it has been decided it will be a microkernel. Not in the old Mach sense of the word, but in...
30
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None,...
34
by: Guch Wu | last post by:
Boost has many terrific libraries. But I want to know whether they are ready for using in real projects. Which of them are mature enough, or just only in progress?
14
by: dE|_ | last post by:
For months I have been happily using a downloaded JS file and inline script to embed and control the parameters of SWF files which I use as mp3 players with actionscript. The last so.addParam...
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
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.