如何获取Treeview的最后一个节点?

时间:2015-03-15 04:45:23

标签: vb.net treeview treenode axwindowsmediaplayer

所以...我有一个基本上代表播放列表的树视图,我希望在最后一级(歌曲)获得nodes以获得他们的text并且能够创建一个按照AxWindowsMediaPlayer中歌曲显示顺序的treeview播放列表,树视图有类别,例如按年份,按专辑,按艺术家排列,歌曲将始终位于最后一级,尽管最后一级不会总是一样的。我怎么能得到最后一级节点?谢谢。

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

1 个答案:

答案 0 :(得分:2)

您可以通过设置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")

使用键"歌曲":

查找所有节点
Dim songNodes As TreeNode() = myTreeView.Nodes.Find("Song", searchAllChildren:=True)

以下是一个例子:

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

输出到立即窗口:

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