473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 48933
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave | last post by:
How do I check in a Windows Forms app if any controls have changed? I have a form that collects data, and I want to prompt the user if they try to exit the app, or load a new file, without saving...
8
by: Alison | last post by:
Hi, Al I am trying to design a user interface which provides both menus and toolbars for some users to click on whatever they want to do, at the same time, I would like to have a console window...
1
by: VictorT | last post by:
Hi All, I am trying to create a simple Windows form that lists a users' data one user at a time with the usual "Next" & "Previous" buttons. Upon loading the form, I am able to populate all...
5
by: Rob | last post by:
Hi to all. I am new to Visualbasic.net and windowz programming in general so I hope someone can help. I am trying to get info from a csv (Excell) file into a windows form. For instance I want to...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
4
by: newbie | last post by:
Hi, I have a windows form written in C#. The form has multiple textboxes and multiple buttons. I want to create a form where when the users clicks on a particulat textbox and then clicks on a...
1
by: inpuarg | last post by:
I have a windows form, there are button , textbox label etc. controls on it. and i can programmatically get each of their names. and i also have a printPreviewDialog on a form. I cannot get it...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
2
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using Visual Studio C# windows. Every time I want to use some of the methods in System.Windows.Form such as Clear(), it gives me error message and saying "System.Windows.Forms.TextBox does...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.