473,683 Members | 3,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 39598

"Ant" <An*@discussion s.microsoft.com > wrote in message
news:F8******** *************** ***********@mic rosoft.com...
Hi,
I'd like to do this basically:

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


void ClearText(Contr ol 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(Contr ol 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(Contr ol c)
{
foreach(Control cc in Controls)
{
Textbox tb = cc as Textbox;
if( tb != null )
tb.Text = string.Empty;

if ( cc.Controls.Cou nt >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
1990
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 neccessary in nearly every collection. But with generics I consider this feature of foreach as dangerous. Casting is now almost always unwanted. interface IFoo{} class Foo:IFoo{} class FooBar:IFoo{}
8
2106
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 case with a regular for loop when the second expression is false when the loop begins). Is there an explanation for this?
16
2241
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 rmg(string key) { return resource.GetString("key"); } private Literal ctrl_txt()
2
1616
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
10068
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 condidate for this though I'm having problems getting the right syntax/form. I've been trying variations on the following: foreach (DropDownList myDDL in Controls) { //do something here
3
1875
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 through all the other controls. thanks
3
2682
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 determine/alter the order it works in?
3
11375
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) remove these items, but I noticed that not all of my buttons are getting removed from the screen. _The Code This is a sample of how I'm CREATING the buttons: for (int i = 0; i < arrButtonData.GetLength(0); i++) { //Make new button
9
2564
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 dialog with several such User Controls. Each custom control has a property for COM port name (serial port). Each custom control has a method for writing the settings. The user control has a GetEnumeration structure so it can be easily referenced...
0
8504
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9042
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8922
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8751
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6429
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5789
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4519
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.