472,133 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

How to clear all TextBox in a Windows Form?

Hi,

In my C# Windows Form MyForm, it has many TextBoxes, e.g., txtName, txtID,
txtAddress, ...,
it also has many ComboBoxes, e.g., cboState, cboGender.
How do I clear all those controls's Text in a function? Is it possible to
do it in foreach loop?
Thanks for help.

Jason
Jan 18 '06 #1
5 48764
Hi Jason,
it is possible to clear all them at once but you will have to write
recursive function becaues some controls could be in subcontainers

you could use

public ClearConent(Control parent)
foreach(Control ctl in parent.Controls)
{
//clear contained controls
if (ctlControls.Count > 0) ClearConent(ctl);

if(ctl is Textbox) ((TextBox)ctl).Text = string.Empty;
elseif (ctl is CheckBox) ((CheckBox))ctl).Checked = false;

}

Hope this help
Galin Iliev [MCSD.NET]

Jan 18 '06 #2
Hi Jason,

Try this loop:
private void button1_Click(object sender, EventArgs e)

{

foreach (Control c in this.Controls)

if (c is TextBox)

(c as TextBox).Clear();

}
Regards - Octavio
In my C# Windows Form MyForm, it has many TextBoxes, e.g., txtName, txtID,
txtAddress, ...,
it also has many ComboBoxes, e.g., cboState, cboGender.
How do I clear all those controls's Text in a function? Is it possible to
do it in foreach loop?
Thanks for help.

Jason

Jan 18 '06 #3
Thanks!
How about some TextBoxes and ComboBoxes in a GoupBox?

"Galcho[MCSD.NET]" <ga****@gmail.com>
???????:11**********************@g49g2000cwa.googl egroups.com...
Hi Jason,
it is possible to clear all them at once but you will have to write
recursive function becaues some controls could be in subcontainers

you could use

public ClearConent(Control parent)
foreach(Control ctl in parent.Controls)
{
//clear contained controls
if (ctlControls.Count > 0) ClearConent(ctl);

if(ctl is Textbox) ((TextBox)ctl).Text = string.Empty;
elseif (ctl is CheckBox) ((CheckBox))ctl).Checked = false;

}

Hope this help
Galin Iliev [MCSD.NET]

Jan 18 '06 #4
Hi,

"Jason Huang" <Ja************@hotmail.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
Thanks!
How about some TextBoxes and ComboBoxes in a GoupBox?

It will work the same, also with Panel , and any other grouping control.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 18 '06 #5
Thanks Galcho!
What does this code "ctl.Controls.Count > 0" mean?

"Galcho[MCSD.NET]" <ga****@gmail.com>
???????:11**********************@g49g2000cwa.googl egroups.com...
Hi Jason,
it is possible to clear all them at once but you will have to write
recursive function becaues some controls could be in subcontainers

you could use

public ClearConent(Control parent)
foreach(Control ctl in parent.Controls)
{
//clear contained controls
if (ctlControls.Count > 0) ClearConent(ctl);

if(ctl is Textbox) ((TextBox)ctl).Text = string.Empty;
elseif (ctl is CheckBox) ((CheckBox))ctl).Checked = false;

}

Hope this help
Galin Iliev [MCSD.NET]

Jan 22 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Rod Gill | last post: by
4 posts views Thread by newbie | last post: by
2 posts views Thread by =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | 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.