By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,686 Members | 2,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,686 IT Pros & Developers. It's quick & easy.

How to delete form controls in a loop

P: n/a
J L
I need to loop through a form's controls collection and delete some
based on type and location. I have tried For..Each and For i = 0 to
form.controls.count - 1 but when I delete any it messes up the loop.

What is the correct way to go about this. I thought this was answered
before but can not find it.

TIA,
John
Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Maybe this can help you:

For i As Integer = 0 To Me.Controls.Count - 1
Try
Me.Controls.Remove(Me.Controls(0))
Catch ex As Exception
End Try
Next

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.

"J L" <jo**@marymonte.com> schreef in bericht
news:nh********************************@4ax.com...
I need to loop through a form's controls collection and delete some
based on type and location. I have tried For..Each and For i = 0 to
form.controls.count - 1 but when I delete any it messes up the loop.

What is the correct way to go about this. I thought this was answered
before but can not find it.

TIA,
John

Nov 21 '05 #2

P: n/a
J L wrote:
I need to loop through a form's controls collection and delete some
based on type and location. I have tried For..Each and For i = 0 to
form.controls.count - 1 but when I delete any it messes up the loop.

What is the correct way to go about this. I thought this was answered
before but can not find it.

TIA,
John


That loop will fail after you're removed about half of the controls

maybe something like:

while form.controls.count>1
form.Controls(0).dispose()
wend

--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #3

P: n/a
JL,

The most secure method (the sample is without childcontrols), this allows as
well to keep selective some controls in it

\\\
For i As Integer = Me.Controls.Count - 1 to 0 step -1
Me.Controls.Remove(Me.Controls(0))
Next
///

I hope this helps,

Cor
Nov 21 '05 #4

P: n/a
Peter,
For i As Integer = 0 To Me.Controls.Count - 1
Try
Me.Controls.Remove(Me.Controls(0))
Catch ex As Exception
End Try
Next


Brrrrrrrrrrrrrrrr a not handled catched error.
You like the movie "the deerhunter" I assume

:-)

Cor

Nov 21 '05 #5

P: n/a
Cor,

it was early for me this morning and late last night so that's a double
excuse lol ;-)

Greetz Peter

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"Cor Ligthert" <no************@planet.nl> schreef in bericht
news:uJ**************@tk2msftngp13.phx.gbl...
JL,

The most secure method (the sample is without childcontrols), this allows as well to keep selective some controls in it

\\\
For i As Integer = Me.Controls.Count - 1 to 0 step -1
Me.Controls.Remove(Me.Controls(0))
Next
///

I hope this helps,

Cor

Nov 21 '05 #6

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
The most secure method (the sample is without childcontrols), this allows
as well to keep selective some controls in it

\\\
For i As Integer = Me.Controls.Count - 1 to 0 step -1
Me.Controls.Remove(Me.Controls(0))
Next
///


If all controls should be removed, you can call the 'Controls' collection's
'Clear' method instead of removing the controls in a loop.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7

P: n/a
J L
Hi Cor,
Thank you that worked perfectly. I do not remove all controls but
processing the loop top-down did catch them all. Going the other way
only caught half the controls and then threw an error.

Thanks again,
John

On Tue, 3 May 2005 10:20:26 +0200, "Cor Ligthert"
<no************@planet.nl> wrote:
JL,

The most secure method (the sample is without childcontrols), this allows as
well to keep selective some controls in it

\\\
For i As Integer = Me.Controls.Count - 1 to 0 step -1
Me.Controls.Remove(Me.Controls(0))
Next
///

I hope this helps,

Cor


Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.