Loop Through Tab Pages and Remove

tommyready

Member
Joined
Jul 1, 2009
Messages
23
Programming Experience
3-5
Hey,

I'm trying to figure out how to loop through a tabcontrol and remove certain tab pages dynamically. Can anyone help me with this?

Something like

VB.NET:
        For i = 1 To DataArea.Controls.Count
            DataArea.Controls.RemoveAt(i)
        Next

Any help would be great, Thanx!!:confused:
 
TabControl.TabPages property returns a TabPageCollection, for this you can use one of the three different Remove methods, see TabControl.TabPageCollection Members (System.Windows.Forms)

Looping a collection and at the same time deleting items from it is probably easiest done by iterating index from (Count -1) to 0 Step -1, depending on how you are organizing things..
 
Ok, I have a TabControl with 6 tabs. 5 of them are created dynamically, I need to remove those tabs dynamically as well. So I figured the index of the first dynamically created tab = 1. So should I be able to just loop and delete.

VB.NET:
Private Sub RemoveTabs()

   For i = 1 To DataArea.TabPages.Count
        DataArea.TabPages.RemoveAt(i)
   Next

End Sub
 
The first post sounds as if you only want to remove specific tabpages. There are several ways of doing so such as removing by index or looking for the page by name or text properties.

A sub for removing tab pages by the TabPage.Name specified :

VB.NET:
[COLOR="Blue"]Public Sub [/COLOR]RemoveTabPageByName(myTabControl [COLOR="blue"]As [/COLOR]TabControl, strTabName [COLOR="blue"]As [/COLOR]String)
    
    [COLOR="blue"]For Each [/COLOR]page [COLOR="blue"]As [/COLOR]TabPage [COLOR="blue"]In [/COLOR]myTabControl.TabPages
        [COLOR="blue"]If [/COLOR]page.Name = strTabName [COLOR="blue"]Then[/COLOR]
            myTabControl.TabPages.Remove(page)
        [COLOR="blue"]End If[/COLOR]
    [COLOR="blue"]Next [/COLOR]page

[COLOR="blue"]End Sub[/COLOR]

A sub for removing tab pages by the displayed tabs text specified :

VB.NET:
[COLOR="Blue"]Public Sub[/COLOR] RemoveTabPageByText(myTabControl [COLOR="blue"]As [/COLOR]TabControl, strTabText [COLOR="blue"]As [/COLOR]String)
    
    [COLOR="blue"]For Each [/COLOR]page [COLOR="blue"]As [/COLOR]TabPage [COLOR="blue"]In [/COLOR]myTabControl.TabPages
        [COLOR="blue"]If [/COLOR]page.Text = strTabText [COLOR="blue"]Then[/COLOR]
            myTabControl.TabPages.Remove(page)
        [COLOR="blue"]End If[/COLOR]
    [COLOR="blue"]Next [/COLOR]page

[COLOR="blue"]End Sub[/COLOR]

To remove all your dynamic pages you can simply specify to exclude the original pages since you already know there hard coded names such as:

VB.NET:
[COLOR="Blue"]For Each[/COLOR] page [COLOR="blue"]As [/COLOR]TabPage [COLOR="blue"]In [/COLOR]myTabControl.TabPages
    [COLOR="SeaGreen"]'Exclude your design time created page(s)[/COLOR]
    [COLOR="blue"]If [/COLOR]page.Name = [COLOR="DarkRed"]"TabPage1"[/COLOR] [COLOR="blue"]Then Continue For[/COLOR]
    myTabControl.TabPages.Remove(page)
[COLOR="blue"]Next [/COLOR]page
 
The loop you have in your last post would cause an error. With a complete total of 6 tabpages; your looping 5 times in this example, removing a page with each itteration. By the time it gets to itteration #4, you have removed 3 tab pages with 3 pages remaining; so there is no longer any tab with an index of 4 to remove at this point.
 
Back
Top