我构建了一个从我的网络驱动器填充的treenode,从这个树视图我想填充另一个树视图以显示第一个选择时的文件。例如,如果用户单击c:\ TestFolder,则第二个树视图将显示TestFolder,所有子文件夹和文件。以下是我的代码,谢谢。
Imports System
Imports System.IO
Public Class F_Treeview_Demo
Public IsSomethingChecked As Boolean = False
Private fbIgnoreClick As Boolean = False
Private fbLoadingForm As Boolean = True
Private sRead As String
Public n As TreeNode
Public l As New List(Of String)
Public drivesTree As New List(Of String)
Private Sub F_Treeview_Demo_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Initialize the local directory treeview
Dim nodeText As String = ""
Dim sb As New C_StringBuilder
With My.Computer.FileSystem
For i As Integer = 0 To .Drives.Count - 1
'** Build the combo box with drive's node text
sb.ClearText()
sb.AppendText(.Drives(i).DriveType.ToString)
sb.AppendText(RemoveTrailingChar(.Drives(i).Name, gtBACKSLASH))
cmbDrives.Items.Add(sb.FullText)
Next
End With
End Sub
Public Sub FillTree(ByVal s As String)
Dim nodeText As String = ""
Dim sb As New C_StringBuilder
With My.Computer.FileSystem
For i As Integer = 0 To .Drives.Count - 1
'** Build the drive's node text
sb.ClearText()
sb.AppendText(.Drives(i).DriveType.ToString)
sb.AppendText(RemoveTrailingChar(.Drives(i).Name, gtBACKSLASH))
nodeText = sb.FullText
'nodeText = Me.tvFolders.SelectedNode.Text
'Check to see if DropDown Selection is the same as what has been read into i
If (sb.FullText = s) Then
'** Add the drive to the treeview
Dim driveNode As TreeNode
tvFolders.Nodes.Clear()
driveNode = tvFolders.Nodes.Add(nodeText)
driveNode.ImageIndex = 0
driveNode.SelectedImageIndex = 1
driveNode.Tag = .Drives(i).Name
Dim FolderNode As String = ""
'Dim FirstNode As TreeNode
'tvFolders.Nodes.Clear()
'FirstNode = tvFolders.Nodes.Add(nodeText)
'** Add the next level of subfolders
ListLocalSubFolders(driveNode, .Drives(i).Name)
driveNode = Nothing
End If
Next
End With
End Sub
Private Sub tvwLocalFolders_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) _
Handles tvFolders.BeforeExpand
' Display the path for the selected node
' lblLocalPath.Text = e.Node.Tag
' Populate all child nodes below the selected node
Dim parentPath As String = AddChar(e.Node.Tag)
tvFolders.BeginUpdate()
Dim childNode As TreeNode = e.Node.FirstNode
'this i added
Dim smallNode As TreeNode = e.Node.FirstNode
Do While childNode IsNot Nothing
ListLocalSubFolders(childNode, parentPath & childNode.Text)
childNode = childNode.NextNode
''this i added
ListLocalFiles(smallNode, parentPath & smallNode.Text)
Loop
tvFolders.EndUpdate()
tvFolders.Refresh()
' Select the node being expanded
tvFolders.SelectedNode = e.Node
End Sub
Private Sub ListLocalFiles(ByVal ParentNode As TreeNode, ByVal PParentPath As String)
Dim FileNode As String = ""
Try
For Each FileNode In Directory.GetFiles(PParentPath)
Dim smallNode As TreeNode
smallNode = ParentNode.Nodes.Add(FilenameFromPath(FileNode))
With smallNode
.ImageIndex = 0
.SelectedImageIndex = 1
.Tag = FileNode
End With
smallNode = Nothing
Next
Catch ex As Exception
End Try
End Sub
Private Sub ListLocalSubFolders(ByVal ParentNode As TreeNode, ByVal ParentPath As String)
' Add all local subfolders below the passed Local treeview node
Dim FolderNode As String = ""
Try
For Each FolderNode In Directory.GetDirectories(ParentPath)
Dim childNode As TreeNode
childNode = ParentNode.Nodes.Add(FilenameFromPath(FolderNode))
With childNode
.ImageIndex = 0
.SelectedImageIndex = 1
.Tag = FolderNode
End With
childNode = Nothing
Next
Catch ex As Exception
End Try
End Sub
Private Sub lblLocalPath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub grpLocalFileSystem_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grpLocalFileSystem.Enter
End Sub
Private Sub cmbDrives_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDrives.SelectedIndexChanged
'populate tree view from user selection
FillTree(Me.cmbDrives.SelectedItem.ToString)
End Sub
Private Sub checkBox_isSelected()
End Sub
Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
ListBox1.Items.Clear()
CallRecursive(tvFolders)
If (IsSomethingChecked = False) Then
MessageBox.Show("Please select an item to replicate.")
End If
End Sub
'End Function
Private Sub PrintRecursive(ByVal n As TreeNode)
System.Diagnostics.Debug.WriteLine(n.Text)
If (n.Checked = True) Then
IsSomethingChecked = True
sRead = n.FullPath
l.Add(sRead)
'If (n.Checked = False) Then
' ListBox1.Items.Add(sRead)
' MessageBox.Show(n.FullPath)
'End If
ListBox1.Items.Add(sRead)
' MessageBox.Show(sRead)
' Next
End If
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)
If IsSomethingChecked = True Then
Exit Sub
End If
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
ListBox1.Items.Clear()
End Sub
Private Sub tvFiles_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvFiles.BeforeSelect
FillTree(Me.tvFolders.SelectedNode.Tag)
End Sub
Private Sub tvFolders_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvFolders.NodeMouseClick
End Sub
Private Sub tvFiles_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFiles.AfterSelect
End Sub
End Class
答案 0 :(得分:1)
据我所知,你已经创建了一个包含节点的树视图,当选择一个节点时,那个带有所有节点的节点应该显示在另一个TreeView
中?
如果是这样,只需添加NodeMouseClick
事件处理程序,然后添加代码:
treeView2.Nodes.Clear()
treeView2.Nodes.Add(Ctype(e.Node.Clone(), TreeNode))
如果第一个TreeView
尚未包含目录中的实际文件,则必须在此之后添加代码以循环遍历treeView2中的所有节点并调用类似Directory.GetFiles
的内容添加带有文件名的子节点。
为了将来参考,最好在问题中添加代码,但我建议尽可能将其减少到真正相关的位。