Say I have TextBox1, TextBox2, TextBox3...etc is there a better way to clear all of them then this...

Posted on 2004-10-21
Last Modified: 2006-11-17
Hey guys,

say I have 4 text boxes, in this case i'll just say they are TextBox1, TextBox2, TextBox3 and TextBox4. Now lets say I want to write a small routine that will clear all of them

would i have to do

TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";

Or is there a way to do it in a for loop that way I don't have the redundant code?


Question by:PoeticAudio
    LVL 20

    Accepted Solution

    You can go over all Form (for Win app), respectively Page (for ASP.NET) controls and if the given control is a text box - clear it. However this will clear all text boxes on the form/page. Otherwise you could add all the text boxes to a collection and go over it like this:

    ArrayList tbs = new ArrayList();
    tbs.Add (TextBox1);
    tbs.Add (TextBox2);
    tbs.Add (TextBox3);
    tbs.Add (TextBox4);

    foreach (TextBox tb in tbs)
      tb.Text = "";

    This is worth only if you often have to handle the same collection of text boxes. If it's only ones, you better make it your way
    LVL 18

    Assisted Solution

    A Bit easier then you're doing:

    TextBox1.Text = TextBox2.Text = TextBox3.Text = TextBox4.Text = "";
    LVL 9

    Expert Comment


                      foreach(Control ctrl in this.Controls)
                            if(ctrl.GetType().ToString().IndexOf("TextBox") > 0)
                                  ctrl.Text = String.Empty;

    S Sansanwal

    Expert Comment


    Control c = new Control;
    Foreach (c in this.Controls)
                 if (c.GetType = TextBox)
                         ctrl.Text = "";


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Learn The Basics of Ethical Hacking & Pen Testing

    Computer and network security is one of the fastest growing and most essential industries in technology, meaning companies will pay big bucks for ethical hackers. This is the perfect course to leap into this lucrative career, learning how to use ethical hacking to reveal ...

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    This video Micro Tutorial is the first in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles al…
    Want to pick and choose which updates you receive? Feel free to check out this quick video on how to manage your email notifications.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now