2010-06-24 3 views
1

내 네트워크 드라이브에서 채울 수 있도록 treenode를 작성했습니다.이 트리보기에서 첫 번째 파일을 선택할 때 파일을 표시하기 위해 다른 treeview를 채우고 싶습니다. 예를 들어 사용자가 c : \ TestFolder를 클릭하면 두 번째 트리 뷰에 TestFolder, 모든 하위 폴더 및 파일이 표시됩니다. 아래는 내 코드입니다, 감사합니다.treenode에서 treenode 채우기

수입 시스템 수입 System.IO 공공 클래스 F_Treeview_Demo 공공 IsSomethingChecked 부울 부울 = 거짓 개인 fbIgnoreClick으로 = 거짓 개인 fbLoadingForm 문자열로 부울 = 사실 개인 SREAD 공공 N의 TreeNode으로 공공 리터로

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 

최종 클래스

,369 (문자열) 새로운 목록으로 공공 drivesTree (문자열) 새로운 목록

답변

1

노드가있는 트리 뷰를 만들었고 노드가 선택되면 그 노드가 모두 자식이되어야합니다. TreeView?

그렇다면, 바로 NodeMouseClick 이벤트 처리기를 추가 한 다음 코드를 추가합니다

treeView2.Nodes.Clear() 
treeView2.Nodes.Add(Ctype(e.Node.Clone(), TreeNode)) 

을 처음 TreeView 이미 당신은 또한 이후에 코드를 추가해야 할 것 디렉토리의 실제 파일을 포함하지 않는 경우 TreeView2의 모든 노드를 반복하고 Directory.GetFiles과 같은 이름을 호출하고 파일 이름이있는 자식 노드를 추가합니다.

나중에 참조 할 수 있도록 질문에 코드를 추가하는 것이 좋지만 가능한 경우 관련성이 높은 비트로만 잘라내려는 것이 좋습니다.

+0

대단히 감사합니다! 이것은 내가 실제로 찾고 있던 것입니다. – jpavlov

+0

이 오류가 발생합니다 : 보호 된 메모리를 읽거나 쓰려고 시도했습니다. 이것은 종종 다른 메모리가 손상되었다는 표시입니다. 트리를 복제하고 노드를 강조 표시 한 후이를 수신합니다. – jpavlov

+0

@jpavlov : 트리 뷰 2에서 노드를 강조 표시하면됩니까? 일부 데이터가 제대로 복제되지 않을 수도 있습니다 (아마 노드 또는 태그의 태그에 넣은 이미지 또는 데이터 일 수 있습니다). 오류가 발생하는 행을 정확히 찾으십시오 ('BeforeExpand' 핸들러 또는 이와 유사한 것으로 가정하여 일부 중단 점에 넣고 단계별로 처리하십시오). –

관련 문제