Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.07/5 (7 votes)
See more:
code for clearing all textbox in a form c#
Posted

The other solutions do not take into account that the controls can be nested (I personally always put a number of Panels (splitters, etc.) and add other controls as Panel's children).

So, the solution needs to be recursive:

C#
void ClearTextBoxes(Control parent) {
    foreach (Control child in parent.Controls) {
        TextBox textBox = child as TextBox;
            if (textBox == null)
                ClearTextBoxes(child);
            else
                textBox.Text = string.Empty;
    } //loop
} //ClearTextBoxes

//...

ClearTextBoxes(myForm);


—SA
 
Share this answer
 
Comments
Tarun.K.S 1-Jun-11 15:51pm    
Aah right, recursion. Perfect solution. 5+
@RagiGopi: This is the answer.
Sergey Alexandrovich Kryukov 1-Jun-11 16:09pm    
Thank you, Tarun.
--SA
ambarishtv 2-Jun-11 1:25am    
+5 :thumbsup:
Sergey Alexandrovich Kryukov 2-Jun-11 1:38am    
Thank you.
--SA
[no name] 15-Jul-15 3:19am    
what is the use of ClearTextBoxes(myForm) ?
Try:
foreach (Control c in Controls)
   {
   TextBox tb = c as TextBox;
   if (tb != null)
      {
      tb.Text = string.Empty;
      }
   }
 
Share this answer
 
Comments
Ragi Gopi 1-Jun-11 5:50am    
not working
StM0n 1-Jun-11 5:52am    
Could you please tell us, what does not work?
Аslam Iqbal 1-Jun-11 6:02am    
my 5
ambarishtv 1-Jun-11 6:11am    
+5
Sergey Alexandrovich Kryukov 1-Jun-11 14:54pm    
Not exactly. Text Boxes may be children of some nested controls, such as Panels, so the correct solution must be recursive.

Please see my solution. (Just 4 for yours this time :-)
--SA
Try this:

XML
foreach (TextBox t in this.Controls.OfType<TextBox>())
            {
                t.Text = string.Empty;
            }
 
Share this answer
 
Comments
Ragi Gopi 1-Jun-11 5:50am    
its not working
RakeshMeena 1-Jun-11 6:16am    
what error/issue you are getting?
ambarishtv 1-Jun-11 6:24am    
i think textbox's placed in groupbox or panel like containers then
use this.Controls replaced as groupbox.Controls
RakeshMeena 1-Jun-11 6:36am    
Yes you are correct. My mistake. But to get a control at nth level would require good amt of logic. So I think at the time of writing code, the developer should be well aware of the containers he would be looking in and code accordingly.

For the down-voter: Before downvoting you should post your problem/issue in detail. It's SICK!!!
ambarishtv 1-Jun-11 6:05am    
good 5+
Here you go

Using C#[^]

Using Javascript[^]
 
Share this answer
 
C#
foreach (Control field in container.Controls)
            {
                if (field is TextBox)
                    ((TextBox)field).Clear();
                else if (field is ComboBox)
                    ((ComboBox)field).SelectedIndex=0;
                else
                    dgView.DataSource = null;
                    ClearAllText(field);
            }
 
Share this answer
 
v2
Comments
CHill60 15-Jul-15 7:44am    
Please refrain from posting solutions to old questions that are already resolved. You will only attract downvotes and your account may be suspended as a result
Sergey Alexandrovich Kryukov 15-Jul-15 8:18am    
There is nothing wrong with answering old questions, but only if the new answer has some value. In this case, this is pure abuse, not a solution. "ClearAllText" is not defined...
What's the purpose, to show that combo box can also be "cleared". Well, it's clear. What else? :-)
—SA
CHill60 15-Jul-15 8:40am    
I usually leave them alone if they add value, a better explanation, a new way of doing things in later versions etc. But yes, this post is just abuse - member has been making a habit of it and been warned by a few of us. I'm giving them one last chance before reporting the account
Sergey Alexandrovich Kryukov 15-Jul-15 8:50am    
If this is a repeated activity, even after warnings, I would say, the last chance is already lost. This person got two abuse reports on the member account so far.

Also, those "answers" are just copies of the code nearly irrelevant to the question.

But next step would be reporting it to the forum. Only it should be properly formulated. This is not answering to old question, which would be perfectly fine, but this is more like an attempt to parasite on the answers of others. Wes Aday called this member a "rep hunter".

—SA
[no name] 16-Jul-15 1:41am    
Are you serious ? one mail id is enough to create new account :)
now you go ;)
But anyway i'm sorry guys.. i will avoid this behaviour..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900