Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to clear containts of all textboxes on a form by issusing one command TIA

1 view
Skip to first unread message

CobraS...@al.com

unread,
Jan 21, 2004, 12:22:32 PM1/21/04
to

Tim Wilson

unread,
Jan 21, 2004, 12:31:15 PM1/21/04
to
You can create this functionality yourself by running through the Controls
collection of the Form:

foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Clear();
}
}

--
Tim Wilson
.Net Compact Framework MVP

<CobraS...@al.com> wrote in message
news:bumcgo$sbl$1...@newsg3.svr.pol.co.uk...
>
>
>


Herfried K. Wagner [MVP]

unread,
Jan 21, 2004, 1:33:57 PM1/21/04
to
* "Tim Wilson" <Tim_Wilson@[DieSpamDie]Rogers.com> scripsit:

> You can create this functionality yourself by running through the Controls
> collection of the Form:
>
> foreach (Control ctrl in this.Controls)
> {
> if (ctrl is TextBox)
> {
> ((TextBox)ctrl).Clear();
> }
> }

Note: This won't clear nested textboxes...

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Tim Wilson

unread,
Jan 21, 2004, 2:35:23 PM1/21/04
to
True. Recursion is the way to go to clear every textbox directly or
indirectly on the form. This was just a simple piece of code I threw
together to demo the basic understanding of how to locate and clear
textbox's dynamically. Thanks for pointing this out as I should have in my
original post :)

--
Tim Wilson
.Net Compact Framework MVP

"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message
news:bumgpd$jcp8p$2...@ID-208219.news.uni-berlin.de...

Herfried K. Wagner [MVP]

unread,
Jan 21, 2004, 2:39:16 PM1/21/04
to
* "Tim Wilson" <Tim_Wilson@[DieSpamDie]Rogers.com> scripsit:
> True. Recursion is the way to go to clear every textbox directly or
> indirectly on the form. This was just a simple piece of code I threw
> together to demo the basic understanding of how to locate and clear
> textbox's dynamically. Thanks for pointing this out as I should have in my
> original post :)

No problem -- it was only a little note -- maybe a "TODO" for the OP...

;-)

Tim Wilson

unread,
Jan 21, 2004, 3:00:53 PM1/21/04
to
Ok, so I broke down and wrote the recursive code:

private void button1_Click(object sender, System.EventArgs e)
{
ClearAllTextBoxes(this);
}

private void ClearAllTextBoxes(Control parent)
{
if (parent is TextBox)
{
((TextBox)parent).Clear();
}
foreach (Control ctrl in parent.Controls)
{
ClearAllTextBoxes(ctrl);
}
}

... I am easily susceptible to peer pressure :)

--
Tim Wilson
.Net Compact Framework MVP

"Herfried K. Wagner [MVP]" <hirf-spa...@gmx.at> wrote in message

news:bumkjq$j99fv$2...@ID-208219.news.uni-berlin.de...

0 new messages