加载整盘目录到TreeView,注意逐层展开

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 shuwork 的 blog

'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

本文关键:目录 TreeView
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top