Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

control disposal 2

Status
Not open for further replies.

zatch

MIS
Oct 11, 2001
75
US
I have a form with eight textbox controls on it. I have the following procedure which should delete each of the textboxes. What it really does is delete every other textbox on the form. I click a button to call the "Clear_Tbox" procedure and what I'm left with are Textbox2, textbox4, textbox6, and textbox8. Anyone have an idea why this might be?

Private Sub Clear_Tbox()
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
ctl.Dispose()
End If
Next
End Sub
 
The controls collection can be a bit finiky, try looping through the collection from top to bottom.

Code:
    Private Sub Clear_Tbox()
        Dim i as integer 
        For i = Me.Controls.count to 0
            If TypeOf Me.Controls(i) Is TextBox Then
                Me.Controls(i).Dispose()
            End If
        Next
    End Sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks for your reply, Rick.

I changed your line: For i = Me.Controls.count - 1 to 0

to: For i = 0 to me.controls.count - 1

but it still did not work. Now I eventually get the error:

"Specified argument was out of the range of valid values." It is looking for controls which it can't find in the control collection it seems.

I'm sure I've done this before. Let me know if you have any other ideas.

Thanks again.

Zatch

 
what rick meant to say was this

Code:
Private Sub Clear_Tbox()
        Dim i as integer 
        For i = (Me.Controls.count-1) to 0 step -1
            If TypeOf Me.Controls(i) Is TextBox Then
                Me.Controls(i).Dispose()
            End If
        Next
    End Sub

wich means that it will loop through the collection backwards and will avoid the problem you had.



Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Ah yes but you mean well. (is that the correct saying?)

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Very nice. I've not stepped backward before. Thank you both.

Zatch
 
I've not stepped backward before

It's called moonwalking the MJ-way.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top