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

Foreach with controls

Ant
Hi,
I'd like to do this basically:

foreach (Textbox txt in Controls)
{
txt.text = ""
}
How fo I do it?

Thanks very much for any answers.

Ant
Jan 27 '06 #1
7 39583

"Ant" <An*@discussions.microsoft.com> wrote in message
news:F8**********************************@microsof t.com...
Hi,
I'd like to do this basically:

foreach (Textbox txt in Controls)
{
txt.text = ""
}
How fo I do it?


void ClearText(Control c)
{
foreach(Control cc in Controls)
{
Textbox tb = cc as Textbox;
if( tb != null )
tb.Text = string.Empty;
else
ClearText(cc); // recursion required for panels/groups etc
}
}

Jan 27 '06 #2
foreach (Control control in this.Controls)
{
if ( control is Textbox )
control.text = "";
}

Jan 27 '06 #3
Chris S. wrote:
foreach (Control control in this.Controls)
{
if ( control is Textbox )
control.text = "";
}


Isn't this control read only?
Jan 27 '06 #4
Chris S. wrote:
foreach (Control control in this.Controls)
{
if ( control is Textbox )
control.text = "";
}


Isn't this "control" read only?
Jan 27 '06 #5
kangkai1980 wrote:
Chris S. wrote:
foreach (Control control in this.Controls)
{
if ( control is Textbox )
control.text = "";
}


Isn't this control read only?


The variable is - so you can't say "control = new Control()". However,
that doesn't stop you from setting properties, calling methods etc.

Jon

Jan 27 '06 #6
Hi,
void ClearText(Control c)
{
foreach(Control cc in Controls)
{
Textbox tb = cc as Textbox;
if( tb != null )
tb.Text = string.Empty;
else
ClearText(cc); // recursion required for panels/groups etc
}
}


I would change it by
void ClearText(Control c)
{
foreach(Control cc in Controls)
{
Textbox tb = cc as Textbox;
if( tb != null )
tb.Text = string.Empty;

if ( cc.Controls.Count >0 )
ClearText(cc); // recursion required for panels/groups etc
}
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 27 '06 #7
Jon Skeet [C# MVP] wrote:
kangkai1980 wrote:
Chris S. wrote:
foreach (Control control in this.Controls)
{
if ( control is Textbox )
control.text = "";
}

Isn't this control read only?


The variable is - so you can't say "control = new Control()". However,
that doesn't stop you from setting properties, calling methods etc.

Jon


It seems I had misunderstood the foreach thingy, Thanks!
Feb 3 '06 #8

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

Similar topics

13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
8
by: Brad Wood | last post by:
When I do this: foreach( Button btn in myForm.Controls ) An exception is raised when no buttons exist. I would simply expect execution to continue after the foreach loop (just as would be the...
16
by: Islam Elkhayat | last post by:
In my web application i use satalite assembly to localize my web form.. Instead of set the each control text to the resourcemanager getstring() method i try to use foreach loop private string...
2
by: Alan Silver | last post by:
Hello, I have code like the following... foreach (Control ctl in Page.Controls) { if (ctl.ID.StartsWith("X_")) { // do stuff } }
2
by: Italian Pete | last post by:
Hi, I have some dropdownlist web controls on as aspx page. I want to loop through them on first (!IsPostBack) Page_Load in order to populate them. The foreach control structure seems to be a good...
3
by: Varangian | last post by:
Hello how come foreach(ImageButton imgbtn in Page.Controls) doesn't work ? whats wrong in that statement? what should I do? I need to go through each ImageButton in the Page, without going...
3
by: JamesB | last post by:
Hi, I have a foreach... on a bunch of checkboxes in a groupbox. What order will this work in? I have to leave my processing on two of the checkboxes until after the others... is there any way to...
3
AHayes
by: AHayes | last post by:
_Background I'm attempting to build a C# Windows application for work where I need to dynamically create and remove menu items (buttons). I've figured out how to dynamically create and (I think)...
9
tlhintoq
by: tlhintoq | last post by:
Situation: A Windows Form with a single User Control containing 4 custom controls. This lets me drop the User Control on its own form to make it a dialog, or make it part of a larger configuration...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.