473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Clearing all of the textboxes on a form

foreach (TextBox tb in this.Controls)
tb.Dispose();

What this does is clears every other textbox on the form.

Suppose there are 10 textboxes... this will dispose the 1st one, then i
guess whatever list of textboxes it's maintaining gets shifted, so the
2nd is now the 1st in the list, so when it goes to the next one, it is
destroying the 3rd textbox, leaving the 2nd one safe...

any better way to do this so that it destroys all 10 textboxes?

Aug 8 '06 #1
9 2150
Hi,

<Mi************@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
foreach (TextBox tb in this.Controls)
tb.Dispose();

What this does is clears every other textbox on the form.
Not really, most probably it will throw an exception at some point, the
reason is that most probably at least one control will not be a Textbox
This is teh correct code

foreach (Control tb in this.Controls)
if ( tb is TextBox)
tb.Dispose();
or

foreach (IDisposable tb in this.Controls)
tb.Dispose();

What I cannot understand is why you are calling Dispose

What do you want to do anyway, why are you calling Dispose?

Aug 8 '06 #2
Hi,
Where r u from? I have a friend who is also named Michel Suarez

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 8 '06 #3
by clear, do you mean clear the text? if so, why are you calling dispose? if
you want to just clear all text, do this

foreach (TextBox tb in this.Controls)
tb.Text = null;
that sets the text to nothing
--
-iwdu15
Aug 8 '06 #4
foreach (Control tb in this.Controls)
if ( tb is TextBox)
tb.Dispose();

worked the same as

foreach (TextBoxtb in this.Controls)
tb.Dispose();

which only disposes every other textbox.

just to clarify, when i said "clear", i meant destroy.

Its a user control that dynamically creates a bunch of textboxes when
it loads. Before it loads, i want to destroy any textboxes that were
previously created.

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
Where r u from? I have a friend who is also named Michel Suarez

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 8 '06 #5
origionally from NY..

There are a lot of us Mike Suarez's around.... I've even encountered a
few myself.

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
Where r u from? I have a friend who is also named Michel Suarez

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 8 '06 #6
Hi,

Not you them, both me & my friend are from cuba

Glad to help you though.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<Mi************@gmail.comwrote in message
news:11**********************@n13g2000cwa.googlegr oups.com...
origionally from NY..

There are a lot of us Mike Suarez's around.... I've even encountered a
few myself.

Ignacio Machin ( .NET/ C# MVP ) wrote:
>Hi,
Where r u from? I have a friend who is also named Michel Suarez

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Aug 8 '06 #7
Hi,
<Mi************@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
foreach (Control tb in this.Controls)
if ( tb is TextBox)
tb.Dispose();

worked the same as

foreach (TextBoxtb in this.Controls)
tb.Dispose();

which only disposes every other textbox.
Not at all, if Controls contain another contain that does not derive from/is
a TextBox you will get an exception , your code is error prone, the one I
gave you is not.
just to clarify, when i said "clear", i meant destroy.
Ok, I though u mean to clear the Text property.
Its a user control that dynamically creates a bunch of textboxes when
it loads. Before it loads, i want to destroy any textboxes that were
previously created.
Please note that after you call Dispose you should not access the control
again, this mean you should remove it from the Controls list

Frankly I do not see the need to the code you have, in any case you should
call Dispose of the usercontrol
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 8 '06 #8
"Mi************@gmail.com" <Mi************@gmail.comwrote in
news:11**********************@m79g2000cwm.googlegr oups.com:
Its a user control that dynamically creates a bunch of textboxes when
it loads. Before it loads, i want to destroy any textboxes that were
previously created.
I would do the following:

ArrayList al = new ArrayList();
foreach(Control c in this.Controls)
{
if (c is TextBox) al.Add(c);
}
foreach(TextBox tb in al) this.Controls.Remove(tb);

I wouldn't specifically worry about Dispose'ing them - the GC should take
care of that in due time.

-mdb
Aug 8 '06 #9
Hi,
ArrayList al = new ArrayList();
foreach(Control c in this.Controls)
{
if (c is TextBox) al.Add(c);
}
foreach(TextBox tb in al) this.Controls.Remove(tb);
I would do this.

But IMHO all this is useless, all the OP has to do is dispose the
usercontrol, the control itself will takes care of the child controls
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Savvas | last post by:
Hi everybody, I have a lot of textboxes on my form and a "Clear" button. Is there a way with a for loop or something to clear the textboxes, instead of writing textboxName.clear? Thanks a lot
4
by: Ali | last post by:
I used to clear my page's control in Visual Studio 2003 using code like this: Dim c As Control For Each c In Page.Controls(1).Controls If TypeOf c Is TextBox Then CType(c, TextBox).Text = Nothing...
1
by: Alex | last post by:
Hi I'm a beginner in ASP.NET. I'm using 'AddContact.aspx' form to add a contact to DB, 'litStatus' literal to show the result and Server.Transfer to clear inputs in the form. The problem is i...
1
by: Shahid Juma | last post by:
Hi, I have a form and when the user clicks save, it will add all the details to the database. However, when I add it to the database, how do I clear the form values? What happens is that it...
2
by: Eytch | last post by:
Hey: I am a rookie developer so please bear with me. I want to develop a small application for my office but I am having trouble clearing a page that has required fields. If I make a textbox...
5
by: Stephen D Cook | last post by:
I'm trying to clear a form after the user clicks the Insert button so the textboxes are empty and the checkboxes are unchecked. When I try to put the code in the Click part of the Insert button, I...
3
by: cmrhema | last post by:
This is reference to Vandana Sridhar's refreshing the form http://www.thescripts.com/forum/thread619976.html The code displayed below did work. Dim Ctrl As Control For Each Ctrl In...
5
jgroos
by: jgroos | last post by:
Here is the situation: I have 25 textboxes that are all handled by the same TextChanged event handler. My problem is, when I click my "Clear" button on the form, it clears all the textboxes and...
2
by: rsteph | last post by:
I've got a form set up that has a series of textboxes to enter employee info, with a listbox at the bottom listing all the employees with their title (to make it easier to find employees to edit...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.