Looping through a collection of textboxes or buttons

  • Thread starter Thread starter Belee
  • Start date Start date
B

Belee

I always clear textboxes by using eg this.txtName.clear();
What is the best way of doing this to about 20 boxes or
asignig values to them in C#.
 
Hi

To tell you the true I have never use TextBox.Clear() but I could bet that
what it does is set TextBox.Text="" , well maybe no using the public
property but the internal variable.

When I want to do that I do something like this ( taken from real code ,
all the controls are System.Web.UI.WebControl.TextBox):
TravellingRoadTXT.Text =

EstimatedSpeedTXT.Text =

DriverLicenceStateTXT.Text =

PostedSpeedTXT.Text = "";


If this is faster/ more efficient way of doing it, I don't know for sure, I
do know it works great :)

Hope this help,
 
I always clear textboxes by using eg this.txtName.clear();
What is the best way of doing this to about 20 boxes or
asignig values to them in C#.

What does it mean "the best way" ?
The fastest or the easiest ?
The easiest, if they on the same control, by using Controls collections.

Gawel
 
Belee said:
I always clear textboxes by using eg this.txtName.clear();
What is the best way of doing this to about 20 boxes or
asignig values to them in C#.

Hi,

Try the following....it loops through all the controls on the current
form, verifies the control is a text box, then issues a command to run
the "clear" method.

yourFormName frm = this;
foreach (Control tx in frm.Controls)
{
if (tx.GetType() == typeof(System.Windows.Forms.TextBox))
{
tx.clear();
}
}
 
Back
Top