![]() |
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 |
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.
|
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();Ps: how can this be posted as "c# code"? |
Re: Clearing multiple textbox's text
Quote:
:
Quote:
|
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! |
Re: Clearing multiple textbox's text
What about trying the following approach?
:
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. |
Re: Clearing multiple textbox's text
May I ask what are you doing with 50 textboxes?
|
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