469,353 Members | 2,053 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

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 48552
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.