473,799 Members | 3,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 49014
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(Con trol parent)
foreach(Control ctl in parent.Controls )
{
//clear contained controls
if (ctlControls.Co unt > 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(o bject 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.c om>
???????:11***** *************** **@g49g2000cwa. googlegroups.co m...
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(Con trol parent)
foreach(Control ctl in parent.Controls )
{
//clear contained controls
if (ctlControls.Co unt > 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******** ******@TK2MSFTN GP12.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.C ount > 0" mean?

"Galcho[MCSD.NET]" <ga****@gmail.c om>
???????:11***** *************** **@g49g2000cwa. googlegroups.co m...
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(Con trol parent)
foreach(Control ctl in parent.Controls )
{
//clear contained controls
if (ctlControls.Co unt > 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
11365
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 changes that have been made. In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification messages, etc. But, this doesn't seem to happen in Windows Forms. I tried Spy-ing a windows forms app, and the WM_COMMAND messages don't even get sent...
8
18658
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 available in the same form for users to enter commands and display outputs if some prefer to use character based user interface. I would like to implement the user interface in vb .net. Now I have menus and toolbars ready, but do not now how to get a...
1
1456
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 textboxes however when clicking either of the buttons, nothing happens - the data from the next/previous rows does not re-populate the textboxes. This is an extremely simple situation yet I cannot figure out what is going wrong. Can anyone help? I've...
5
1775
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 get the totals of one "column" and put it into a textbox on a form when I press a button. I did not think it would be difficult to do , but I cant seem to get it right. I have managed to get a datagrid working , using the oledbDataAdapter...
4
3160
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 further away, completely leaving the selector box area. Any ideas? VS 2003 and VB.Net This is a simple application at the moment but the form is inherited from a
4
1605
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 button, a text is added to that textbox. How do i go about doing this when a user selects on any textbox and clicks on a buttont to insert text, it insert the text in the correct
1
1662
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 through loop. i know that it does not have an appearance. so - how can i get it programmatically ? private void button1_Click(object sender, EventArgs e)
3
4698
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 form that has the System.Windows.Forms.LinkLabel controls on it is in a different project and under a different namespace from the file where the LinkLabel_LinkClicked events are, so I can't just do frm.ShowDialog under the LinkClicked method. ...
2
1995
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 not contain a definition for 'Clear'". Could someone help me with that. This happen for other methods that are used in many places of the sources I am using but when I want to use them it gives me error message. Thanks -- Nejadian
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10238
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.