3、先在窗体的加载事件中写如下代码:
Dim mynode As New TreeNode
mynode = TreeView1.Nodes.Add("我的计算机")
mynode.Tag = "mycomputer"
这段代码在TreeView中建立了一个节点--“我的计算机”
利用节点node的Tag属性来存放文件夹的完整地址,便于后面在ListView中查询该文件夹下的文件。
4、测试一下代码效果。
5、接着在以上代码下写:
Dim drives As DriveInfo() = DriveInfo.GetDrives
Dim submynode As New TreeNode
For Each drive As DriveInfo In drives
submynode = mynode.Nodes.Add(drive.Name)
submynode.Tag = drive.Name
Next
这段代码的作用是遍历所有驱动器,并将驱动器的名称添加到TreeView中,形成一个驱动器的树形结构图。
1、写一个子程序或者函数用来添加驱动器下的各个文件夹在TreeView上的节点。该子程序如下:
Private Sub CreateTree(mynode As TreeNode, path As String)
Dim dir As DirectoryInfo = New DirectoryInfo(path)
Dim submynode As New TreeNode
For Each subdir As DirectoryInfo In dir.GetDirectories
submynode = mynode.Nodes.Add(subdir.Name)
submynode.Tag = subdir.FullName
Next
End Sub
第六节:
1、建立TreeView的NodeMouseClick事件,在该事件中处理图标状态。在NodeMouseClick事件中写如下代码:
Dim mynode As TreeNode = e.Node
If mynode.IsExpanded = False Then
mynode.SelectedImageIndex = 0
Else
mynode.SelectedImageIndex = 1
End If