Re: Clear all TextBox on a Form
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 14 Aug 2007 15:57:23 -0400
static void ClearTextBoxes(Control.ControlCollection controls)
{
// Cycle through the controls.
foreach (Control control in controls)
{
// The textbox.
TextBoxBase textBox = (control as TextBoxBase);
// If there is a textbox, then clear it.
if (textBox != null)
{
// Set the text to an empty string.
textBox.Text = "";
}
}
}
Note that this will clear RichTextBoxes as well. If you want to only
clear textboxes, then you need to change the "(control as TextBoxBase)"
statement to "(control as TextBox)".
Furthermore, if you have a need to clear composite controls, then you
will have to iterate through the controls collection of each control you
come across at the top level, and so on, and so on until there are no more
children.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"RP" <rpk.general@xxxxxxxxx> wrote in message
news:1187120796.743372.77410@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Oh, I want that itself. Can you please illustrate on how to use
ControlCollection?
On Aug 15, 12:38 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
No, there isn't. You will have to cycle through all the textboxes on
the form, and then clear the text out yourself. It should be easy enough
to
do, given a ControlsCollection instance, you can use the "as" operator to
determine if the control is a TextBox or not (or, TextBoxBase). If it
is,
then you can set the Text property to an empty string.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: Clear all TextBox on a Form
- From: RP
- Re: Clear all TextBox on a Form
- References:
- Clear all TextBox on a Form
- From: RP
- Re: Clear all TextBox on a Form
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Clear all TextBox on a Form
- From: RP
- Clear all TextBox on a Form
- Prev by Date: Re: forms collection?
- Next by Date: Re: Integrating VB 2005 module.
- Previous by thread: Re: Clear all TextBox on a Form
- Next by thread: Re: Clear all TextBox on a Form
- Index(es):
Relevant Pages
|