View Single Post
Old May 3rd, 2008, 3:11 PM   #5
Robocop
Programmer
 
Join Date: Sep 2004
Posts: 47
Rep Power: 0 Robocop is on a distinguished road
Re: Clearing multiple textbox's text

Quote:
Originally Posted by Jimbo View Post
There's a couple problems with this code. First, you need to add the controls to the collection, not the Text properties. Also, you can't add them by giving a string with the control's name (unfortunately ). Also, you should use the length of the ArrayList instead of hardcoding a length, to make sure you get each one. Lastly, an optional recommendation, you could use a generic collection so that you don't have to use the Object type for each element. For exmaple:
c# Syntax (Toggle Plain Text)
  1. List<TextBox> theBoxes = new List<TextBox>();
  2. // when you create a new textbox, add it
  3. TextBox foo = new TextBox();
  4. theBoxes.Add(foo);
  5. // later on, just go over each element in the arraylist and set each of their text properties
  6. foreach (TextBox t in theBoxes)
  7. {
  8. t.Text = String.Empty;
  9. }
  10. // alternatively:
  11. //for(int i = 0; i < theBoxes.Count; i++)
  12. // theBoxes[i].Text = String.Empty;


Instead of just using [code] you can often put [code=language] to get colored syntax. There are some anomalies as I recall, such as C++ being [code=cpp]. For C#, it's just [code=c#] though.
From what I can see, the main difference between the ArrayList and List is that with the List you need to specify a class, and with the ArrayList you don't specify a type, am I right?

Also, maybe I didn't explain myself well, but I was looking for an alternative to having a line of code for each textbox, eg:

theBoxes.Add(answer1);
theBoxes.Add(answer2);
theBoxes.Add(answer3);

and so on...

Thanks alot for the help!
Robocop is offline   Reply With Quote