Search code examples
vb.nettreeviewtreenodeaxwindowsmediaplayer

How to get the last nodes of a Treeview?


So... I have a treeview that basically represents a playlist, and I want to get the nodes on the last level (the songs) to get their text and be able to create a Playlist for a AxWindowsMediaPlayer in the order the songs appear in the treeview, the treeview has categories, for example, by year, by album, by artist, the songs will always be on the last level, although the last level will no be always the same. How could I get the last level nodes? Thank you.

  -- Artist
       --> ALbum
           --> Song1
           --> Song2
           --> Song3
       --> album2
           --> Song1
           --> Song2
  -- Artist2
       --> ALbum1
           --> Song1

Solution

  • You can make nodes searchable by setting its name / key.

    '                    key         text
    albumNode.Nodes.Add("Song", "Name of song 1")
    albumNode.Nodes.Add("Song", "Name of song 2")
    albumNode.Nodes.Add("Song", "Name of song 3")
    

    To find all nodes with the key "Song":

    Dim songNodes As TreeNode() = myTreeView.Nodes.Find("Song", searchAllChildren:=True)
    

    Here's an example:

    Using view As New TreeView
    
        For i As Integer = 1 To 2
           'Add a new artist node with the key "Artist"
            With view.Nodes.Add("Artist", String.Format("Artist {0}", i))
                Debug.WriteLine(.Text)
                For j As Integer = 1 To 2
                   'Add a new album node with the key "Album"
                    With .Nodes.Add("Album", String.Format("Album {0}-{1}", i, j))
                        Debug.WriteLine("    " & .Text)
                        For k As Integer = 1 To 3
                           'Add a new song node with the key "Song"
                            With .Nodes.Add("Song", String.Format("Song {0}-{1}-{2}", i, j, k))
                                Debug.WriteLine("        " & .Text)
                            End With
                        Next
                    End With
                Next
            End With
        Next
    
        Debug.WriteLine("")
        Debug.WriteLine("Nodes with ""Song"" key:")
        Debug.WriteLine("")
    
        For Each node As TreeNode In view.Nodes.Find("Song", searchAllChildren:=True)
            Debug.WriteLine(node.Text)
        Next
    
    End Using
    

    Output to the Immediate Window:

    Artist 1
        Album 1-1
            Song 1-1-1
            Song 1-1-2
            Song 1-1-3
        Album 1-2
            Song 1-2-1
            Song 1-2-2
            Song 1-2-3
    Artist 2
        Album 2-1
            Song 2-1-1
            Song 2-1-2
            Song 2-1-3
        Album 2-2
            Song 2-2-1
            Song 2-2-2
            Song 2-2-3
    
    Nodes with "Song" key:
    
    Song 1-1-1
    Song 1-1-2
    Song 1-1-3
    Song 1-2-1
    Song 1-2-2
    Song 1-2-3
    Song 2-1-1
    Song 2-1-2
    Song 2-1-3
    Song 2-2-1
    Song 2-2-2
    Song 2-2-3