Hi I am trying to enumerate through 3 levels of directory structure/folders and create TabPages with the names same as the directory names. Further I want to add controls to these TabPages created at runtime.
I have tried to create TabPages with the following commands:

Dim tabcontrol1 As New TabControl
        Dim tabpagecollection1 As New TabControl.TabPageCollection(tabcontrol)

        tabpagecollection1.Add("Page1")
        tabcontrol1.TabPages.Add("TabPage1", "Page2")

But with the above commands I can't add controls to the TabPages and if I use the following command:

Dim tabpage1 As New TabPage

        tabcontrol1.Controls.Add(tabpage)

Then I can't figure out how to declare variables dynamically.

Any suggestions?

Recommended Answers

All 6 Replies

Try this:

Public Class Form1
    Dim Count As Int16 = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NewTab As New TabPage
        Dim NewTextBox As New TextBox
        NewTab.Name = "new tab " & CStr(Count)
        NewTab.Text = NewTab.Name
        NewTextBox.Text = CStr(Count)
        Count += 1
        Me.TabControl1.Controls.Add(NewTab)
        NewTab.Controls.Add(NewTextBox)
        Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
    End Sub
End Class

Hi Wayne
Thanks for the reply, but it does not help me in the current scenario. My fault, I should have explained the scenario in detail. So here it is:
I have 2 folders to start with in the current path: Alpha, Beta.
Alpha contains 3 folders: AB, AC, AD.
And finally AB contains folders: XY, YZ, ZX.
Now going though the folder structure while getting the folder names and creating tabs Alpha and Beta is not a problem. Problem arises when I try to create the tabpage AB within the tabpage Alpha.

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim slvl1 As String
        Dim slvl2 As String
        Dim slvl3 As String
        Dim tabcontrol1 As New TabControl
        Dim tabpage1 As New TabPage
        Dim pathcoll As New ArrayList()
        Dim arrvlvl1 As String() = Directory.GetDirectories("c:\mypath\")
        For Each slvl1 In arrvlvl1
            tabpage1.Name = slvl1
            tabpage1.Text = slvl1
            tabcontrol1.Controls.Add(tabpage1)
            Dim arrlvl2 As String() = Directory.GetDirectories(slvl1)
            'How to add a tab to the pages Alpha & Beta?
            For Each slvl2 In arrlvl2
                Dim arrlvl3 As String() = Directory.GetDirectories(slvl2)
                For Each slvl3 In arrlvl3
                    pathcoll.Add(slvl3)
                Next
            Next
        Next
        Me.Controls.Add(tabcontrol1)
    End Sub
End Class

Now how to add a tab to the tabpages Alpha & Beta?

You can't add tabs to tabs. You will have to add another tab control to the tab page and add your tabs to them.

How is it possible to do the same in the current scenario?

Solved :icon_cheesygrin:

Imports System.IO Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim arrlvl1 As String() = Directory.GetDirectories("C:\MYPATH\") Dim form_main As Control = CType(sender, Control) Dim x, y, z As Integer x = 0 y = 0 z = 0 Dim tcacc As New TabControl Dim tcsrv(x) As TabControl Dim tcchar(y) As TabControl Dim tbacc(x) As TabPage Dim tbsrv(y) As TabPage Dim tbchar(z) As TabPage tcacc.Size = New Size(500, 200) For Each slvl1 As String In arrlvl1 ReDim Preserve tbacc(x) tbacc(x) = New TabPage ReDim Preserve tcsrv(x) tcsrv(x) = New TabControl With tcacc .Controls.Add(tbacc(x)) .TabPages.Item(x).Text = SplitPath(slvl1) .TabPages.Item(x).Name = .TabPages.Item(x).Text .TabPages.Item(x).Controls.Add(tcsrv(x)) End With tcsrv(x).Size = tcacc.Size Dim serverEntries As String() = Directory.GetDirectories(slvl1) For Each slvl2 As String In serverEntries ReDim Preserve tcchar(y) tcchar(y) = New TabControl ReDim Preserve tbsrv(y) tbsrv(y) = New TabPage With tcsrv(x) .Controls.Add(tbsrv(y)) .TabPages.Item(y).Text = SplitPath(slvl2) .TabPages.Item(y).Controls.Add(tcchar(y)) End With tcchar(y).Size = tcsrv(x).Size Dim charEntries As String() = Directory.GetDirectories(slvl2) For Each slvl3 As String In charEntries ReDim Preserve tbchar(z) tbchar(z) = New TabPage With tcchar(y) .Controls.Add(tbchar(z)) .TabPages.Item(z).Text = SplitPath(slvl3) .TabPages.Item(z).Name = .TabPages.Item(z).Text End With z += 1 Next z = 0 y += 1 Next y = 0 x += 1 Next Me.Controls.Add(tcacc) End Sub End Class

Forgot to add a little function i am using above.

Public Function SplitPath(ByVal strpath As String) As String
        Dim arrsrtpath As String() = strpath.Split(CChar("\"))
        strpath = arrsrtpath(arrsrtpath.Length - 1)
        Return strpath
End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.