By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,478 Members | 1,833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,478 IT Pros & Developers. It's quick & easy.

Clear all TextBox on a Form

P: n/a
RP
I want to clear all the Text Boxes on the Form. Is there any short
method to avoid clearing them one by one.

Aug 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"RP" <rp*********@gmail.comwrote in message
news:11**********************@j4g2000prf.googlegro ups.com...
>I want to clear all the Text Boxes on the Form. Is there any short
method to avoid clearing them one by one.

Aug 14 '07 #2

P: n/a
RP
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...@spam.guard.caspershouse.comwrote:
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...@spam.guard.caspershouse.com

Aug 14 '07 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"RP" <rp*********@gmail.comwrote in message
news:11*********************@i38g2000prf.googlegro ups.com...
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...@spam.guard.caspershouse.comwrote:
> 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...@spam.guard.caspershouse.com


Aug 14 '07 #4

P: n/a
RP
How to call this function from say, ButtonClick()

On Aug 15, 12:57 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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 = "";
}
}

}
Aug 14 '07 #5

P: n/a
On Aug 14, 4:05 pm, RP <rpk.gene...@gmail.comwrote:
How to call this function from say, ButtonClick()

On Aug 15, 12:57 am, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
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 = "";
}
}
}- Hide quoted text -

- Show quoted text -
private void button1_Click(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
if (control is TextBox)
{
TextBox textBox = control as TextBox;
textBox.Clear();
}
}
}

Aug 15 '07 #6

P: n/a
The last code is not enough, think on container within a container, so using
the simple control collection is not good, you should also check whether its
a container which also have its own private controls to delete (collection of
user controls).
--
Sincerely
Yaron Karni
http://dotnetbible.blogspot.com/
"xt******@gmail.com" wrote:
On Aug 14, 4:05 pm, RP <rpk.gene...@gmail.comwrote:
How to call this function from say, ButtonClick()

On Aug 15, 12:57 am, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
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 = "";
}
}
}- Hide quoted text -
- Show quoted text -

private void button1_Click(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
if (control is TextBox)
{
TextBox textBox = control as TextBox;
textBox.Clear();
}
}
}

Aug 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.