471,577 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,577 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 39470

"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
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by

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.