Resolved Problem removing certain tabpages from tabcontrols

delstar

Well-known member
Joined
Jun 9, 2006
Messages
59
Programming Experience
1-3
In my application, I am using several tabcontrols. Some of the tabpages are created during runtime, and I keep a list of these for the purpose of going back and deleting those tabpages. The code I use for this is:

VB.NET:
For Each cntrl As Control In root.Controls
            clearcontrols(cntrl, ClearLists, ClearTabPages)
            .
            .
            .
            If TypeOf cntrl Is TabControl And ClearTabPages = True Then
                For Each tp As String In DynTPList
                    If CType(cntrl, TabControl).TabPages.ContainsKey(tp) Then
                        CType(cntrl, TabControl).TabPages.RemoveByKey(tp)
                    End If
                Next
            End If
        Next

This works in most cases, but there is one tabcontrol that I'm creating dynamic tabpages on that also has several static tabpages. Whenever this subroutine runs, it removes the dynamic tabpages, but it also removes the very last static tabpage in the TC. Multiple executions of the subroutine in the same instance will eventually leave me with no static tabpages. I have done some debugging and watched the DynTPList to make sure that the names of the static pages never appear. They don't.

I am at a loss as to why this is happening. I even have other tabcontrols with mixed static/dynamic pages that this doesn't happen on. They are handled the exact same way.

Any ideas?


Edit: Resolved - I tried switching the logic around so that it loops through the tabpages instead of the list, and it appears to be working properly now. Modified code is:
VB.NET:
 For Each cntrl As Control In root.Controls
            clearcontrols(cntrl, ClearLists, ClearTabPages)
            .
            .
            .
            If TypeOf cntrl Is TabControl And ClearTabPages = True Then
                For Each tp As TabPage In CType(cntrl, TabControl).TabPages
                    If DynTPList.Contains(tp.Name) Then
                        CType(cntrl, TabControl).TabPages.Remove(tp)

                    End If
                Next
            End If
        Next
 
Last edited:
You are forgetting a vital part; disposing the control object that you removed, including it's child controls. Doing that will also simplify the code a bit, since disposing it will also remove it from its parent collection:
VB.NET:
For Each tp As TabPage In CType(cntrl, TabControl).TabPages
    If DynTPList.Contains(tp.Name) Then
        [B]tp.Dispose()[/B]
    End If
Next
Are you still using .Net 2.0 as your profile says? If not, you can filter the Controls collection using the OfType extension:
VB.NET:
For Each tc In root.Controls.OfType(Of TabControl)()
    For Each tp As TabPage In tc.TabPages
 
Thanks for pointing that out. I have updated the code to dispose of the tp, and it seems to work a bit smoother now.

No, I am no longer using 2.0. My current project was started in VS 2008 with the target framework being 3.5, but it has been converted to a VS 2010 project (still 3.5 for target framework, though).
 
Back
Top