Question : VB.NET 2008 Dynamically Add Forms Controls Based on Directories and Files

I'm working on an application that will give end users the ability to run scripts against a SQLServer database.  The form will have tabs for each type of scripts (BO, FA, IR) which are directories. The combo box on each tab will be populated with the scripts from it's respective directory.  Instead of having to change and update the form later if a a new type is added and new scripts are added.  I want to dynamically add tabpages to my tab control on formload according to what directories are in the main directory.  Currently the main directory has "BO" "FA" and "IR" folders.  If I add "ST" I want a new tab to display on formload.

Currently All 3 tabs are getting created but instead of tabs "BO" "FA" and "IR" I'm getting "IR" "FA" and "IR".  I can't figure out why. The tab name for all ends up "tbpIR".  My controls (combobox and label) are not getting added to the tabpages as well.

See code below
   
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        conn.Open()


        Dim cmb As New ComboBox
        Dim tbp As New TabPage
        Dim lbl As New Label
        Dim dir As New DirectoryInfo("\\server\it\ScriptTest\")

        For Each d As DirectoryInfo In dir.GetDirectories

            tbp.Name = "tbp" & d.Name
            tbp.Location = New System.Drawing.Point(4, 22)
            tbp.Padding = New System.Windows.Forms.Padding(3)
            tbp.Size = New System.Drawing.Size(281, 187)
            tbp.UseVisualStyleBackColor = True
            tbp.Text = d.Name
            Me.tbcModules.Controls.Add(tbp)

            cmb.FormattingEnabled = True
            cmb.Location = New System.Drawing.Point(15, 98)
            cmb.Name = "cmb" & d.Name
            cmb.Size = New System.Drawing.Size(228, 21)
            tbp.Controls.Add(cmb)

            lbl.AutoSize = True
            lbl.Location = New System.Drawing.Point(12, 42)
            lbl.Name = "Lbl" & d.Name
            lbl.Size = New System.Drawing.Size(219, 13)
            lbl.TabIndex = 3
            lbl.Text = "Select a job to run from the drop down menu."
            tbp.Controls.Add(lbl)

            Dim folder As New DirectoryInfo("\\server\it\ScriptTest\" & d.Name)
            Dim tmpName As String = ""
            Try
                For Each f As FileInfo In folder.GetFiles
                    cmb.Items.Add(f.Name.Replace(".txt", ""))
                Next


            Catch c As Exception
                tbp.Dispose()
            End Try
        Next
    End Sub

Answer : VB.NET 2008 Dynamically Add Forms Controls Based on Directories and Files

Without testing it, but at a quick glance, I would suggest changing this

Dim cmb As New ComboBox
Dim tbp As New TabPage
Dim lbl As New Label

to this
Dim cmb As ComboBox
Dim tbp As  TabPage
Dim lbl As  Label

and inside the for each loop
set cmb = new ComboBox
etc.

Since it appears you are creating one object and the reference gets changed, rather than creating new objects.
Random Solutions  
 
programming4us programming4us