【发布时间】:2012-03-26 19:06:27
【问题描述】:
我正在尝试使用 TreeView 编写部署工具。我按照我在网上找到的几个教程来使用文件夹/子文件夹/和文件填充树视图。一切正常,我处理文件部署的功能似乎没问题,但我遇到了显示问题。
我的 treeView 可以正确显示我的文件夹结构和每个文件夹中的文件,甚至将正确的图标图像附加到每个文件夹/文件。
如果我单击 + 展开或折叠节点(文件夹),一切仍然正常,但如果我单击文件夹,则 _NodeMouseClick 事件似乎正在触发,并且没有正确刷新我的内容。不再显示任何子文件夹,并且文件现在具有文件夹图标。如果我折叠并重新展开文件夹节点,一切都会恢复原状。
下面是相关代码:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' when our component is loaded, we initialize the TreeView by adding the root node
Dim mRootNode As New TreeNode
mRootNode.Text = RootPath
mRootNode.Tag = RootPath
mRootNode.ImageKey = CacheShellIcon(RootPath)
mRootNode.SelectedImageKey = mRootNode.ImageKey
mRootNode.Nodes.Add("*DUMMY*")
TreeView1.Nodes.Add(mRootNode)
End Sub
Private Sub _BeforeCollapse(sender As Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse
' clear the node that is being collapsed
e.Node.Nodes.Clear()
' and add a dummy TreeNode to the node being collapsed so it is expandable again
e.Node.Nodes.Add("*DUMMY*")
End Sub
Private Sub _BeforeExpand(sender As Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
' clear the expanding node so we can re-populate it, or else we end up with duplicate nodes
e.Node.Nodes.Clear()
AddImages(e)
End Sub
Private Sub _AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
e.Node.Nodes.Clear()
Dim folder As String = CStr(e.Node.Tag)
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder)
e.Node.Nodes.Add(file.Substring(file.LastIndexOf("\"c) + 1))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
我想我需要尝试从 _NodeMouseClick 例程中调用 AddImages 例程,但无法使其工作。 AddImages 接受 TreeViewCancelEventArgs 而我在 _NodeMouseClick 例程中没有。
Private Sub AddImages(ByRef e As System.Windows.Forms.TreeViewCancelEventArgs)
'---[ get the directory representing this node ]---
Dim mNodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
'---[ add each subdirectory from the file system to the expanding node as a child node ]---
For Each mDirectory As IO.DirectoryInfo In mNodeDirectory.GetDirectories
'---[ declare a child TreeNode for the next subdirectory ]---
Dim mDirectoryNode As New TreeNode
'---[ store the full path to this directory in the child TreeNode's Tag property ]---
mDirectoryNode.Tag = mDirectory.FullName
'---[ set the child TreeNodes's display text ]---
mDirectoryNode.Text = mDirectory.Name
mDirectoryNode.ImageKey = CacheShellIcon(mDirectory.FullName)
mDirectoryNode.SelectedImageKey = mDirectoryNode.ImageKey
'---[ add a dummy TreeNode to this child TreeNode to make it expandable ]---
mDirectoryNode.Nodes.Add("*DUMMY*")
'---[ add this child TreeNode to the expanding TreeNode ]---
e.Node.Nodes.Add(mDirectoryNode)
Next
'---[ add each file from the file system that is a child of the argNode that was passed in ]---
For Each mFile As IO.FileInfo In mNodeDirectory.GetFiles
'---[ declare a TreeNode for this file ]---
Dim mFileNode As New TreeNode
'---[ store the full path to this file in the file TreeNode's Tag property ]---
mFileNode.Tag = mFile.FullName
'---[ set the file TreeNodes's display text ]---
mFileNode.Text = mFile.Name
mFileNode.ImageKey = CacheShellIcon(mFile.FullName)
mFileNode.SelectedImageKey = mFileNode.ImageKey & "-open"
'---[ add this file TreeNode to the TreeNode that is being populated ]---
e.Node.Nodes.Add(mFileNode)
Next
End Sub
如果有人有任何提示,我将不胜感激。 谢谢,
【问题讨论】: