![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2004
Posts: 42
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 742
Rep Power: 3
![]() |
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> |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2004
Posts: 42
Rep Power: 0
![]() |
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"? |
|
|
|
|
|
#4 | ||
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 742
Rep Power: 3
![]() |
Re: Clearing multiple textbox's text
Quote:
). 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)
Quote:
![]()
__________________
<insert disclaimer here> <insert shameless plug for Visual Studio here> |
||
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Sep 2004
Posts: 42
Rep Power: 0
![]() |
Re: Clearing multiple textbox's text
Quote:
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! |
|
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 742
Rep Power: 3
![]() |
Re: Clearing multiple textbox's text
What about trying the following approach?
c# Syntax (Toggle Plain Text)
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> |
|
|
|
|
|
#7 | |
|
Professional Programmer
Join Date: May 2006
Location: UK - London
Posts: 327
Rep Power: 3
![]() |
Re: Clearing multiple textbox's text
May I ask what are you doing with 50 textboxes?
__________________
Quote:
|
|
|
|
|
|
|
#8 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 889
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |