469,306 Members | 2,547 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

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 39127

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by cody | last post: by
16 posts views Thread by Islam Elkhayat | last post: by
2 posts views Thread by Italian Pete | last post: by
3 posts views Thread by Varangian | last post: by
3 posts views Thread by JamesB | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.