1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' On s'abonne à l'évènement BeforeExpand du TreeView (on le fait après avoir populé le TreeView)
AddHandler Me.TreeView1.BeforeExpand, AddressOf Me.TreeView1_BeforeExpand
End Sub
Private Sub PopulateTreeView()
Dim rootNode As TreeNode
Dim info As New DirectoryInfo("C:\")
If info.Exists Then
rootNode = New TreeNode(info.Name)
rootNode.Tag = info
rootNode.Expand()
GetDirectories(info.GetDirectories(), rootNode)
TreeView1.Nodes.Add(rootNode)
End If
End Sub
Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, ByVal nodeToAddTo As TreeNode)
Dim aNode As TreeNode
Dim subSubDirs() As DirectoryInfo
Dim subDir As DirectoryInfo
For Each subDir In subDirs
aNode = New TreeNode(subDir.Name, 0, 0)
aNode.Tag = subDir
aNode.ImageKey = "folder"
Me.AddFakeNode(aNode)
Try
subSubDirs = subDir.GetDirectories()
Catch e As UnauthorizedAccessException
subSubDirs = Nothing
End Try
If Not subDir.Attributes = (FileAttributes.Hidden Or subDir.Attributes) Then
nodeToAddTo.Nodes.Add(aNode)
End If
Next subDir
End Sub
Private Sub AddFakeNode(ByVal nodeToAddTo As TreeNode)
' On ajoute le noeud fantome pour afficher le signe +
nodeToAddTo.Nodes.Insert(0, New TreeNode(String.Empty))
End Sub
Private Sub RemoveFakeNode(ByVal nodeToRemoveFrom As TreeNode)
' On supprime le noeud fantome
nodeToRemoveFrom.Nodes.RemoveAt(0)
End Sub
Private Sub TreeView1_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs)
RemoveFakeNode(e.Node)
' On charge le premier niveau de dossiers pour le noeud en cours de traitement
Dim di As New DirectoryInfo(e.Node.FullPath)
GetDirectories(di.GetDirectories(), e.Node)
End Sub
Public Sub New()
InitializeComponent()
PopulateTreeView()
End Sub 'New
Private Sub showCheckedNodesButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Disable redrawing of treeView1 to prevent flickering
' while changes are made.
treeView1.BeginUpdate()
' Collapse all nodes of treeView1.
treeView1.CollapseAll()
' Add the CheckForCheckedChildren event handler to the BeforeExpand event.
AddHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren
' Expand all nodes of treeView1. Nodes without checked children are
' prevented from expanding by the checkForCheckedChildren event handler.
treeView1.ExpandAll()
' Remove the checkForCheckedChildren event handler from the BeforeExpand
' event so manual node expansion will work correctly.
RemoveHandler treeView1.BeforeExpand, AddressOf CheckForCheckedChildren
' Enable redrawing of treeView1.
treeView1.EndUpdate()
End Sub 'showCheckedNodesButton_Click
' Prevent expansion of a node that does not have any checked child nodes.
Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs)
If Not HasCheckedChildNodes(e.Node) Then
e.Cancel = True
End If
End Sub 'CheckForCheckedChildren
' Returns a value indicating whether the specified
' TreeNode has checked child nodes.
Private Function HasCheckedChildNodes(ByVal node As TreeNode) As Boolean
If node.Nodes.Count = 0 Then
Return False
End If
Dim childNode As TreeNode
For Each childNode In node.Nodes
If childNode.Checked Then
Return True
End If
' Recursively check the children of the current child node.
If HasCheckedChildNodes(childNode) Then
Return True
End If
Next childNode
Return False
End Function 'HasCheckedChildNodes
Private Sub treeView1_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
Dim newSelected As TreeNode = e.Node
ListView1.Items.Clear()
Dim nodeDirInfo As DirectoryInfo = CType(newSelected.Tag, DirectoryInfo)
Dim subItems() As ListViewItem.ListViewSubItem
Dim item As ListViewItem = Nothing
Dim dir As DirectoryInfo
For Each dir In nodeDirInfo.GetDirectories()
item = New ListViewItem(dir.Name, 0)
subItems = New ListViewItem.ListViewSubItem() {New ListViewItem.ListViewSubItem(item, "Directory"), New ListViewItem.ListViewSubItem(item, _
dir.LastAccessTime.ToShortDateString())}
item.SubItems.AddRange(subItems)
ListView1.Items.Add(item)
Next dir
Dim file As FileInfo
For Each file In nodeDirInfo.GetFiles()
item = New ListViewItem(file.Name, 1)
subItems = New ListViewItem.ListViewSubItem() {New ListViewItem.ListViewSubItem(item, "File"), New ListViewItem.ListViewSubItem(item, _
file.LastAccessTime.ToShortDateString())}
item.SubItems.AddRange(subItems)
ListView1.Items.Add(item)
Next file
ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
End Sub
'Selection des checkbox avec heritage aux sous dossiers:
Private Sub CheckAllChildNodes(TreeNode As TreeNode, nodeChecked As Boolean)
Dim node As TreeNode
For Each node In TreeNode.Nodes
node.Checked = nodeChecked
If node.Nodes.Count > 0 Then
'Si le nud actuel possède des nuds enfant, appelez la méthode CheckAllChildsNodes récursive.
Me.CheckAllChildNodes(node, nodeChecked)
End If
Next (node)
End Sub
Private Sub node_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
'Le code s'exécute uniquement si l'utilisateur a provoqué l'état activé au changement .
If e.Action <> TreeViewAction.Unknown Then
If e.Node.Nodes.Count > 0 Then
'Appelle la méthode de CheckAllChildNodes, en passant dans le courant
'valeur Analyse du TreeNode dont vérifié l'état changé.
Me.CheckAllChildNodes(e.Node, e.Node.Checked)
End If
End If
End Sub |
Partager