Search code examples
vb.nettreeviewtreenode

Treeview code cuts after partial completion of code


I have the following code which works for one parent treenode with x-amount of children but I also wanted to create another parent node but thistime round populate it with dwg file names... For some reason it cuts after having only taken account with the xls extentions I also want to add a parent with children names for other type of extentions. following the correctly working code I have added the incorect woring code?

Working code:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim di As New IO.DirectoryInfo("c:\la")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

    For Each dra In diar1

        'Dim sr As StreamReader = New StreamReader(dra.FullName)
        If System.IO.Path.GetExtension(dra.Name).ToLower() = ".xls" Then
            If TreeView1.Nodes.Count = 0 Then TreeView1.Nodes.Add("Offerts")
            TreeView1.Nodes(0).Nodes.Add(dra.Name)
        End If
    Next

End Sub

And now the incorrectly programmed extended code:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim di As New IO.DirectoryInfo("c:\la")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

    For Each dra In diar1

        'Dim sr As StreamReader = New StreamReader(dra.FullName)
        If System.IO.Path.GetExtension(dra.Name).ToLower() = ".xls" Then
            If TreeView1.Nodes.Count = 0 Then TreeView1.Nodes.Add("Offerts")
            TreeView1.Nodes(0).Nodes.Add(dra.Name)
        End If
        If System.IO.Path.GetExtension(dra.Name).ToLower() = ".dwg " Then
            If TreeView1.Nodes.Count = 3 Then TreeView1.Nodes.Add("Technical data")
            TreeView1.Nodes(1).Nodes.Add(dra.Name)
        End If
    Next

End Sub

Solution

  • When it gets to this If statement:

    If TreeView1.Nodes.Count = 0 Then TreeView1.Nodes.Add("Technical data")
    

    It doesn't add the "Technical data" node because TreeView1.Nodes.Count is not zero (it is one) since it has already created the "Offerts" node and one or more nodes under Offerts.

    To do this halfway right (to do it "really" right would probably not be good to get into here), you need to add a key when you create the root nodes, so that you can use the Find function to see if it is already created, and also to put the filenames under the correct nodes. So instead of just TreeView1.Nodes.Add("Offerts"), you add the key as a second parameter: TreeView1.Nodes.Add("Offerts", "Offerts"). Then you can use the TreeView's Find function to do enumerate that node to check if it exists, add child nodes, delete it, etc.

    Dim di As New IO.DirectoryInfo("c:\la")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo
    
    For Each dra In diar1
    
         If System.IO.Path.GetExtension(dra.Name).ToLower() = ".xls" Then
            If TreeView1.Nodes.Find("Offerts", False).Length = 0 Then TreeView1.Nodes.Add("Offerts", "Offerts")
                TreeView1.Nodes.Find("Offerts", False)(0).Nodes.Add(dra.Name)
         End If
    
        If System.IO.Path.GetExtension(dra.Name).ToLower() = ".dwg" Then
            If TreeView1.Nodes.Find("Technical data", False).Length = 0 Then TreeView1.Nodes.Add("Technical data", "Technical data")
                TreeView1.Nodes.Find("Technical Data", False)(0).Nodes.Add(dra.Name)
        End If
    Next