My FAQ,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Visual Basic > 用户界面
加载整盘目录到TreeView,注意逐层展开
作者:shuwork 时间:2001-11-01 10:41 出处:互联网 责编:MyFAQ
              摘要:加载整盘目录到TreeView,注意逐层展开

'shuwork 自Programming Microsoft Visual Basic 6.0 收藏

Option Explicit

' True if Cancel was pressed to close this form
Public CancelPressed As Boolean

Private m_Path As String

' this is used by many routines in the module
Dim FSO As New Scripting.FileSystemObject

Private Sub Form_Load()
    ' build the subdirectory tree
    DirRefresh
End Sub

Private Sub Form_Resize()
    ' the distance among controls
    Const DISTANCE = 100
    Dim tvwTop As Single
   
    ' move the buttons and the label
    lblPath.Move DISTANCE, 0, ScaleWidth, lblPath.Height
    cmdOK.Move ScaleWidth / 2 - DISTANCE - cmdOK.Width, ScaleHeight - DISTANCE - cmdOK.Height
    cmdCancel.Move ScaleWidth / 2 + DISTANCE, cmdOK.Top
    ' resize the treeview control
    ' the Top position depends on the visibility of the lblPath label
    If lblPath.Visible Then
        tvwTop = lblPath.Top + lblPath.Height
    Else
        tvwTop = DISTANCE
    End If
    tvwDir.Move DISTANCE, tvwTop, ScaleWidth - DISTANCE * 2, ScaleHeight - tvwTop - cmdOK.Height - DISTANCE * 2
End Sub

Private Sub DirRefresh()
    ' build the treeview control
    Dim dr As Scripting.Drive
    Dim rootNode As node, nd As node
   
    On Error Resume Next
   
    ' add the "My Computer" root (expanded)
    Set rootNode = tvwDir.Nodes.Add(, , "\\MyComputer", "My Computer", 1)
    rootNode.Expanded = True
   
    ' add all the drives, with a plus sign
    For Each dr In FSO.Drives
        If dr.Path <> "A:" Then
        Err.Clear
        Set nd = tvwDir.Nodes.Add(rootNode.Key, tvwChild, dr.Path & "\", dr.Path & " " & dr.VolumeName, 2)
        If Err = 0 Then AddDummyChild nd
        End If
    Next
   
End Sub

Sub AddDummyChild(nd As node)
    ' add a dummy child node, if necessary
    If nd.Children = 0 Then
        ' dummy nodes' Text property is "***"
        tvwDir.Nodes.Add nd.Index, tvwChild, , "***"
    End If
End Sub

Private Sub tvwDir_Click()
    m_Path = tvwDir.SelectedItem.Key
    lblPath.Caption = tvwDir.SelectedItem.Key
End Sub

Private Sub tvwDir_Expand(ByVal node As ComctlLib.node)
    ' a node if being expanded
    Dim nd As node
    ' exit if the node had been already expanded in the past
    If node.Children = 0 Or node.Children > 1 Then Exit Sub
    ' also exit if it doesn't have a dummy child node
    If node.Child.Text <> "***" Then Exit Sub
    ' remove the dummy child item
    tvwDir.Nodes.Remove node.Child.Index
    ' add all the subdirs of this Node object
    AddSubdirs node
End Sub

Private Sub AddSubdirs(ByVal node As ComctlLib.node)
    ' add all the subdirs under a node
    Dim fld As Scripting.Folder
    Dim nd As node

    ' the path in the node is hold in its key property
    ' cycle on all its subdirectories
    For Each fld In FSO.GetFolder(node.Key).SubFolders
        Set nd = tvwDir.Nodes.Add(node, tvwChild, fld.Path, fld.Name, 3)
        nd.ExpandedImage = 4
        ' if this directory has subfolders, add a "+" sign
        If fld.SubFolders.Count Then AddDummyChild nd
    Next
End Sub

Private Sub cmdOK_Click()
    Unload Me
End Sub

Private Sub cmdCancel_Click()
    CancelPressed = True
    Unload Me
End Sub

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有