Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-10-21
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 20

Accepted Solution

TheAvenger earned 100 total points
ID: 12373021
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

tusharashah earned 100 total points
ID: 12373176
A Bit easier then you're doing:

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

Expert Comment

ID: 12373589

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

S Sansanwal

Expert Comment

ID: 12399914

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


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

604 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