2012-03-26 24 views
1

我正在嘗試使用TreeView編寫部署工具。我遵循了我在網上找到的幾個教程,用文件夾/子文件夾/和文件填充樹視圖。這一切工作和我的功能來處理我的文件部署似乎是好的,但我有一個顯示問題。VB.Net如何在TreeView中刷新節點NodeMouseClick

我的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 

如果有人有任何提示,我將不勝感激幫助。 謝謝,

回答

1

我猜問題是這樣的:

Private Sub _AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect 
    e.Node.Nodes.Clear() 
    '// etc 
End Sub 

它刪除所有你在BeforeExpand事件添加的節點。

+0

天才!我甚至沒有想過看AfterSelect事件!這些代碼現在都在其他地方處理過了,一旦我發表評論,問題就消失了。非常感謝! – tkflick 2012-03-26 20:05:34