Populate TreeView from Array using vb script

Steve Sibley
Steve Sibley used Ask the Experts™
on
I have an array that identifies the level in the Tree that I want.  I know that I need to do a recursive routine, but I keep getting stuck.  I have the first node created.  Now I need to recursively explode the subnodes of each of the level 1 nodes.  Here is the data:
Seq      bom_level      Node_text
0      1      1__1100-079__UMBRELLA SUPPORT
1      2      2__1100-079-003__SUPPORT UMBRELLA
2      3      3__1100-079-003-001__TUBE STAND
3      4      4__TR3C-0150.0009__TUBE_RND_CARBON-C_1.50_.095
4      3      3__1100-079-003-002__PLATE BASE
5      3      3__1100-079-003-003__ANGLE SUPPORT
6      4      4__UA1C-0250.0200.0025__ANGLE_CARBON-H_2.50X2.00_.25
7      4      4__UA1C-0250.0200.0025__ANGLE_CARBON-H_2.50X2.00_.25
8      3      3__1100-079-003-004__BAR SUPPORT
9      4      4__BS1C-0050__BAR_SQR_CARBON-H_.50
10      2      2__O-059-215__U-BOLT
11      2      2__O-060-003-A__WASHER HARD
12      2      2__O-061-003__WASHER LOCK
13      2      2__O-005-016__SCREW HEX
14      2      2__O-062-009__NUT HEX
15      2      2__O-060-005-A__WASHER HARD
16      2      2__O-059-216__PIN QUICK RELEASE
17      2      2__B-005-052__UMBRELLA W/BRACKET
18      1      1__1100-133__BRAKE ASSEMBLY
19      2      2__D-001-035__CALIPER BRAKE
20      2      2__1100-056-002__DISC BRAKE
21      2      2__1100-056-003__PLATE BLOCK MOUNTING
22      2      2__1100-056-004__SCREW HEX
23      2      2__O-076-024-1__SCREW SOC
24      2      2__O-063-012-A__NUT LOCK
25      2      2__O-063-012-A__NUT LOCK
26      2      2__O-060-006-A__WASHER HARD
27      2      2__K-002-292__BOLT FLANGE
28      2      2__K-002-292__BOLT FLANGE
29      1      1__1100-338__COOLER OIL ELECTRIC COLD WEATHER
30      2      2__K-006-037__COOLER OIL REMOTE
31      2      2__B-004-078__SWITCH
32      2      2__O-001-009__SCREW HEX
33      2      2__O-060-003-A__WASHER HARD
34      1      1__1100-339__COOLER OIL ELECTRIC WO COLD WEATHER

The array contains 1370 Seq numbers.  There are 120 nodes with a BOM_LEVEL of 1.  I found an algorithm in C, but can't convert it and make it work.  Help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
is there only 3 columns? I'm confused , does the TEXT include the node too?

0      1      1__1100-079__UMBRELLA SUPPORT

Author

Commented:
Yes.  3 columns:  SEQ (Array Row Number); BOM_LEVEL (Tree Level); and Node Text (The text for the TreeNode)
seq                          Level                                                 Node Text
0                                        1                                        1__1100-079__UMBRELLA SUPPORT
1                                        2                                        2__1100-079-003__SUPPORT UMBRELLA
2                                        3                                        3__1100-079-003-001__TUBE STAND
3                                        4                                        4__TR3C-0150.0009__TUBE_RND_CARBON-C_1.50_.095
4                                        3                                        3__1100-079-003-002__PLATE BASE
5                                        3                                        3__1100-079-003-003__ANGLE SUPPORT
6                                        4                                        4__UA1C-0250.0200.0025__ANGLE_CARBON-H_2.50X2.00_.25
7                                        4                                        4__UA1C-0250.0200.0025__ANGLE_CARBON-H_2.50X2.00_.25
8                                        3                                        3__1100-079-003-004__BAR SUPPORT
9                                        4                                        4__BS1C-0050__BAR_SQR_CARBON-H_.50
10                                      2                                        2__O-059-215__U-BOLT

Output should look like

  1__1100-079__UMBRELLA SUPPORT
           2__1100-079-003__SUPPORT UMBRELLA
                  3__1100-079-003-001__TUBE STAND
                         4__TR3C-0150.0009__TUBE_RND_CARBON-C_1.50_.095
                  3__1100-079-003-002__PLATE BASE
                  3__1100-079-003-003__ANGLE SUPPORT
                         4__UA1C-0250.0200.0025__ANGLE_CARBON-H_2.50X2.00_.25
                         4__UA1C-0250.0200.0025__ANGLE_CARBON-H_2.50X2.00
                  3__1100-079-003-004__BAR SUPPORT
                         4__BS1C-0050__BAR_SQR_CARBON-H_.50
             2__O-059-215__U-BOLT
Well.  I figured it out.  Basically, I found an algorithm written in C that would almost do what I wanted it to do, but I couldn't translate to vb because I wasn't familiar with all the objects.  Yesterday, while perusing this site, I found reference to a software converter.  So, I plugged in the C code and out popped the vb!  Sadly, the object was still in the vb.  After messing around trying to get the data from my Array and failing, I finally decided to dump my array into a list and then ran the algorithm.  It now works.  Here is the solution.

Public Sub Fill_First_Node()

        'load array into list
        L = New List(Of frmBOM)

        For I As Integer = 0 To intRecords - 1
            L.Add(New frmBOM())
            L(I).index = Sublist(I, 0)
            L(I).level = Sublist(I, 1)
            L(I).NodeText = Sublist(I, 2)
        Next

        PopulateBaseNodes()


        tvBOM.EndUpdate()
        tvBOM.Refresh()


    End Sub
    Public Sub populateBaseNodes()
        Dim i As Integer
        tvBOM.Nodes.Clear()
        tvBOM.BeginUpdate()

        For i = 0 To L.Count() - 1
            If L(i).level = 1 Then
                tvBOM.Nodes.Add(L(i).NodeText, L(i).NodeText)
                tvBOM.Nodes(tvBOM.Nodes.Count - 1).Tag = L(i)
            End If
        Next

        For i = 0 To tvBOM.Nodes.Count - 1
            populateChilds(tvBOM.Nodes(i))
        Next

        tvBOM.EndUpdate()
        tvBOM.Refresh()
    End Sub
    Public Sub populateChilds(parentNode As TreeNode)
        Dim parentRed As frmBOM = DirectCast(parentNode.Tag, frmBOM)

        For i As Integer = parentRed.index + 1 To L.Count - 1
            If L(i).level = (parentRed.level + 1) Then
                parentNode.Nodes.Add(L(i).NodeText, L(i).NodeText)
                parentNode.Nodes(parentNode.Nodes.Count - 1).Tag = L(i)
                populateChilds(parentNode.Nodes(parentNode.Nodes.Count - 1))
            End If
            If L(i).level <= parentRed.level Then
                Exit For
            End If
        Next
    End Sub

End Class


I think that the key to the solution was in the " Dim parentRed As frmBOM = DirectCast(parentNode.Tag, frmBOM)".  Also, if anyone else wants to use this, you'll need to make a few Public Declarations at the beginning of the program:

Public index As Integer
    Public level As Integer
    Public NodeText As String
    Public L As List(Of frmBOM)

Author

Commented:
The code works.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial