2016-12-01 111 views
0

我需要循環遍歷Treeview中的所有節點,然後更改它的文本如果我的字符串匹配。我發現你必須做一個遞歸循環,但我不知道如何使用這個。這是遞歸循環:Treeview - 循環所有節點並更改子節點中的文本

Private Sub PrintRecursive(ByVal n As TreeNode) 
     System.Diagnostics.Debug.WriteLine(n.Text) 
     MessageBox.Show(n.Text) 
     Dim aNode As TreeNode 
     For Each aNode In n.Nodes 
      PrintRecursive(aNode) 
     Next 
    End Sub 

    ' Call the procedure using the top nodes of the treeview. 
    Private Sub CallRecursive(ByVal aTreeView As TreeView) 
     Dim n As TreeNode 
     For Each n In aTreeView.Nodes 
      PrintRecursive(n) 

     Next 
    End Sub 

例如,如果我的字符串爲「新建文件夾」我需要通過樹形迴路相同Node.text,這樣我可以改變它。任何幫助讚賞。

回答

1

爲了更改文本,您可以提供舊的和新的文本像這樣的參數:

Private Sub PrintRecursive(ByVal n As TreeNode, oldText As String, newText As String) 
    System.Diagnostics.Debug.WriteLine(n.Text) 
    MessageBox.Show(n.Text) 
    If String.Compare(n.Text, oldText, True) = 0 Then 
     n.Text = newText 
    End If 
    Dim aNode As TreeNode 
    For Each aNode In n.Nodes 
     PrintRecursive(aNode, oldText, newText) 
    Next 
End Sub 

' Call the procedure using the top nodes of the treeview. 
Private Sub CallRecursive(ByVal aTreeView As TreeView, oldText As String, newText As String) 
    Dim n As TreeNode 
    For Each n In aTreeView.Nodes 
     PrintRecursive(n, oldText, newText) 
    Next 
End Sub 

這樣,每個節點被選中的文本,新文本應用,如果現有的文字與您正在查找的文字相符。

+0

謝謝你,它的工作原理。我還找到了一個完全符合我需要的解決方案:http://vbcity.com/blogs/xtab/archive/2014/10/12/find-a-treeview-node-programmatically.aspx – LuckyLuke82