我有一個遞歸的方法來填充TreeView後清理它,當做一個刷新。我通過引用傳遞TreeNode,它會導致節點在TreeView中保持可見和可選狀態,即使Clear從邏輯上將它們從節點列表中刪除。當我改變了通過值傳遞TreeNode的方法時,它正確地允許我清除節點。下面示例中的ByRef必須更改爲ByVal(對於VB.NET)。
Private Sub DisplayPackageSubTree()
trvEntries.Nodes.Clear()
trvEntries.SelectedNode = trvEntries.Nodes.Add("Node0", "Root Node", -1, -1)
DisplayFolderTree(_folderContents, trvEntries.Nodes(0))
trvEntries.ExpandAll()
trvEntries.SelectedNode = trvEntries.Nodes(0)
trvEntries.Refresh()
End Sub
Private Sub DisplayFolderTree(ByVal folderContents As SubFolder, ByRef folderNode As TreeNode)
For Each entry As SubEntry In folderContents.Contents
If TypeOf entry Is SubFolder Then
Dim newNode As TreeNode = folderNode.Nodes.Add(entry.Name, entry.Name, 0, 0)
DisplayFolderTree(entry, newNode)
ElseIf TypeOf entry Is SubDocument Then
folderNode.Nodes.Add(entry.Name, entry.Name, 1, 1)
End If
Next
End Sub
你嘗試過'treeView1.Nodes.Clear()'嗎? –
看着它,它不允許你做.Nodes – Beef
你確定你的'treeView1'是一個TreeView?我沒有'.Items'選項,只有'.Nodes'。 –