Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Clearing multiple textbox's text (http://www.programmingforums.org/showthread.php?t=15758)

Robocop May 3rd, 2008 9:19 AM

Clearing multiple textbox's text
 
I want to clear around 50 textbox's text after a click event is executed.

Do I have to set each textbox's text to empty by writing 50 lines of code? Or is there a better way?

Thanks

Jimbo May 3rd, 2008 12:27 PM

Re: Clearing multiple textbox's text
 
Technically, yes you do. You could possibly simplify it by keeping them all in a collection, and clearing them in a loop.

Robocop May 3rd, 2008 12:34 PM

Re: Clearing multiple textbox's text
 
I tried this, but I think it aint working because this is what it's doing:

"answer1.Text" = string.Empty;

which should be:

answer1.Text = string.Empty;

:

        ArrayList myList = new ArrayList();
        int n = 0;
        int max = 3;
        for (n = 1; n <= max; n++)
        {
            myList.Add("answer" + n + ".Text");
            object Answer = myList[0];
            Answer = string.Empty;
        }


Ps: how can this be posted as "c# code"?

Jimbo May 3rd, 2008 2:07 PM

Re: Clearing multiple textbox's text
 
Quote:

Originally Posted by Robocop (Post 144747)
I tried this, but I think it aint working because this is what it's doing:

"answer1.Text" = string.Empty;

which should be:

answer1.Text = string.Empty;

:

        ArrayList myList = new ArrayList();
        int n = 0;
        int max = 3;
        for (n = 1; n <= max; n++)
        {
            myList.Add("answer" + n + ".Text");
            object Answer = myList[0];
            Answer = string.Empty;
        }


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:
:

  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;


Quote:

Ps: how can this be posted as "c# code"?
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. :)

Robocop May 3rd, 2008 3:11 PM

Re: Clearing multiple textbox's text
 
Quote:

Originally Posted by Jimbo (Post 144756)
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:
:

  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!

Jimbo May 3rd, 2008 3:30 PM

Re: Clearing multiple textbox's text
 
What about trying the following approach?
:

  1. class foobar
  2. {
  3.         private List<TextBox> theBoxes;
  4.  
  5.         private TextBox MakeNewTextbox()
  6.         {
  7.                 TextBox foo = new TextBox();
  8.                 theBoxes.Add(foo);
  9.                 return foo;
  10.         }
  11. }

Then, instead of doing answer1 = new Textbox();, you would do answer1 = MakeNewTextbox();. Then you can set any properties for answer1, and clear all of the textboxes with a loop as discussed above.

As to List<> vs. ArrayList, I prefer the former because when you know the type of objects it contains. List[i] will always return a TextBox (or whatever type was declared), and you don't have to downcast it from Object. ArrayList will hold anything (which can be useful) but if you know the type of each object, you have to cast it before using it. Since in this case, you need to modify each TextBox's Text property, you'll be wanting to pull out variables of type TextBox; it's easier to use the List<> way of doing it.

kruptof May 3rd, 2008 4:11 PM

Re: Clearing multiple textbox's text
 
May I ask what are you doing with 50 textboxes?

lectricpharaoh May 3rd, 2008 4:33 PM

Re: Clearing multiple textbox's text
 
Another benefit to using generics is that it provides greater type safety. If you've got a plain ArrayList to hold your shit, it's only a matter of time until some idiot comes along and puts the wrong type in it. If you're using a generic type, then this will yield a compile-time error, and you can easily fix it.


All times are GMT -5. The time now is 12:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC