Clearing all of the textboxes on a form

  • Thread starter Thread starter Michael.Suarez
  • Start date Start date
M

Michael.Suarez

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?
 
Hi,

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?
 
Hi,


Where r u from? I have a friend who is also named Michel Suarez
 
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
 
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.
 
origionally from NY..

There are a lot of us Mike Suarez's around.... I've even encountered a
few myself.
 
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

origionally from NY..

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


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
 
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
 
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
 
Back
Top