Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 3rd, 2008, 9:19 AM   #1
Robocop
Programmer
 
Join Date: Sep 2004
Posts: 42
Rep Power: 0 Robocop is on a distinguished road
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
Robocop is offline   Reply With Quote
Old May 3rd, 2008, 12:27 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 747
Rep Power: 3 Jimbo is on a distinguished road
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old May 3rd, 2008, 12:34 PM   #3
Robocop
Programmer
 
Join Date: Sep 2004
Posts: 42
Rep Power: 0 Robocop is on a distinguished road
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"?
Robocop is offline   Reply With Quote
Old May 3rd, 2008, 2:07 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 747
Rep Power: 3 Jimbo is on a distinguished road
Re: Clearing multiple textbox's text

Quote:
Originally Posted by Robocop View Post
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:
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;

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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old May 3rd, 2008, 3:11 PM   #5
Robocop
Programmer
 
Join Date: Sep 2004
Posts: 42
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
Old May 3rd, 2008, 3:30 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 747
Rep Power: 3 Jimbo is on a distinguished road
Re: Clearing multiple textbox's text

What about trying the following approach?
c# Syntax (Toggle Plain Text)
  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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old May 3rd, 2008, 4:11 PM   #7
kruptof
Professional Programmer
 
kruptof's Avatar
 
Join Date: May 2006
Location: UK - London
Posts: 329
Rep Power: 3 kruptof is on a distinguished road
Re: Clearing multiple textbox's text

May I ask what are you doing with 50 textboxes?
__________________
Quote:
When I was young it seemed that life was so wonderful,a miracle, oh it was beautiful, magical.
Now watch what you say or they'll be calling you a radical,a liberal, oh fanatical, criminal. Oh won't you sign up your name,we'd like to feel you're acceptable, respectable, oh presentable, a vegetable
kruptof is offline   Reply With Quote
Old May 3rd, 2008, 4:33 PM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 925
Rep Power: 4 lectricpharaoh will become famous soon enough
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.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding more info to a text file without erasing crawforddavid2006 C# 2 Apr 11th, 2007 2:10 PM
[perl] graphical text editor glimmy Existing Project Development 0 Aug 6th, 2006 11:47 PM
How to detect cursor location and insert text??? syntax-error C# 3 Jun 30th, 2005 1:42 AM
Printing rich text with colored background jarrod C# 1 Feb 23rd, 2005 11:48 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:21 AM.

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