473,788 Members | 3,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clear all TextBox on a Form

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
6 8381
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 ControlsCollect ion 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.co m

"RP" <rp*********@gm ail.comwrote in message
news:11******** **************@ j4g2000prf.goog legroups.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
RP
Oh, I want that itself. Can you please illustrate on how to use
ControlCollecti on?

On Aug 15, 12:38 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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 ControlsCollect ion 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.c om

Aug 14 '07 #3
static void ClearTextBoxes( Control.Control Collection 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.co m

"RP" <rp*********@gm ail.comwrote in message
news:11******** *************@i 38g2000prf.goog legroups.com...
Oh, I want that itself. Can you please illustrate on how to use
ControlCollecti on?

On Aug 15, 12:38 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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 ControlsCollect ion 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.c om


Aug 14 '07 #4
RP
How to call this function from say, ButtonClick()

On Aug 15, 12:57 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
static void ClearTextBoxes( Control.Control Collection 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
On Aug 14, 4:05 pm, RP <rpk.gene...@gm ail.comwrote:
How to call this function from say, ButtonClick()

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

<m...@spam.guar d.caspershouse. comwrote:
static void ClearTextBoxes( Control.Control Collection 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(o bject sender, EventArgs e)
{
foreach (Control control in Controls)
{
if (control is TextBox)
{
TextBox textBox = control as TextBox;
textBox.Clear() ;
}
}
}

Aug 15 '07 #6
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...@gm ail.comwrote:
How to call this function from say, ButtonClick()

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

<m...@spam.guar d.caspershouse. comwrote:
static void ClearTextBoxes( Control.Control Collection 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(o bject sender, EventArgs e)
{
foreach (Control control in Controls)
{
if (control is TextBox)
{
TextBox textBox = control as TextBox;
textBox.Clear() ;
}
}
}

Aug 15 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11419
by: T. Wintershoven | last post by:
Hello, I have a few textboxes placed on a SSTab control devided over 3 tabs Is there a way to clear all textboxes in one time in stead of one by one. Someone told me "Use for each item in.........." ??????????? T.i.a.
3
2584
by: Joe | last post by:
I'm wondering how to loop through controls in VB.NET. I have the code from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an example from my VB6 code that loops through controls in a specific frame on a form and unselects the option buttons. .NET barks at this line of code: thiscontrol.value = False Private Sub UnSelectOpts(ByVal passedframeCaption As String) Dim thiscontrol As Control For Each thiscontrol In Me...
6
10296
by: Thonglao Rud | last post by:
I'm trying to clear all textbox on the form. foreach (Control c in this.Controls) { //if (c.GetType() == typeof(TextBox)) if (c is TextBox) { // Found it c.Text = ""; MessageBox.Show(c.Name.ToString());
5
21124
by: nx-2000 | last post by:
I've got a very large C# forms app and now that its being used in bigger environments we're getting a steady stream of "why does it do this?" problems. The most nagging of which right now is that when a MessageBox.Show() is displayed, if the user hits enter or esc, those keys get passed back to the form. From searching online, this is apparently by design(for whom, I don't know, why its not optional, I _really_ don't know). This...
9
12914
by: Peter Afonin | last post by:
Hello: I need to clear all textboxes on the webform after the data has been submitted. How can I do this in one step? I found a C# code: // get a reference to your form control Control frm = FindControl("YourFormID"); foreach(Control ctrl in frm.Controls)
3
6182
by: jw56578 | last post by:
When a page does a post back and there is data in a textbox, that textbox will get automatically repopulated with that data due to the posting of the form. Disabling viewstate has no affected on textbox controls, so how do you make it so that the text box value is cleared out on postback?
13
29188
by: KKuser | last post by:
Hello: I have 20, 30 text and combo boxes on one form, and I would like to clear them up at one time using the statement "With..........End With". I used this when using VB6, but I don't know if I can use the same way in VB.Net. If I can't use With statement in VB.Net, what should I do? Thanks!!
5
49013
by: Jason Huang | last post by:
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.
2
1626
by: Tom | last post by:
I have a textbox on my web form were users can enter in 1 name or many names. The form then validates the name against the database. If the name is valid its then saved into a varaible to be used at a later time. The issue I'm having is as follows: the user enters in 2 names John, Greg clicks my valid button they're both valid so both names are stored in the variable so my variable looks like John,Greg and both are in the textbox. ...
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8993
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
7517
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
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.