home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch08 / explorer / explorer.frm (.txt) next >
Encoding:
Visual Basic Form  |  1998-07-03  |  6.3 KB  |  168 lines

  1. VERSION 5.00
  2. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form ExplorerForm 
  4.    Caption         =   "TreeView and ListView Control Demo"
  5.    ClientHeight    =   5085
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   9240
  9.    LinkTopic       =   "Form1"
  10.    ScaleHeight     =   5085
  11.    ScaleWidth      =   9240
  12.    StartUpPosition =   3  'Windows Default
  13.    Begin VB.Timer Timer1 
  14.       Interval        =   100
  15.       Left            =   -15
  16.       Top             =   4680
  17.    End
  18.    Begin ComctlLib.ListView ListView1 
  19.       Height          =   4470
  20.       Left            =   3600
  21.       TabIndex        =   1
  22.       Top             =   120
  23.       Width           =   5580
  24.       _ExtentX        =   9843
  25.       _ExtentY        =   7885
  26.       View            =   3
  27.       LabelEdit       =   1
  28.       MultiSelect     =   -1  'True
  29.       LabelWrap       =   -1  'True
  30.       HideSelection   =   -1  'True
  31.       FullRowSelect   =   -1  'True
  32.       _Version        =   393217
  33.       ForeColor       =   -2147483640
  34.       BackColor       =   -2147483643
  35.       BorderStyle     =   1
  36.       Appearance      =   1
  37.       NumItems        =   0
  38.    End
  39.    Begin ComctlLib.TreeView TreeView1 
  40.       Height          =   4830
  41.       Left            =   105
  42.       TabIndex        =   0
  43.       Top             =   120
  44.       Width           =   3390
  45.       _ExtentX        =   5980
  46.       _ExtentY        =   8520
  47.       _Version        =   393217
  48.       PathSeparator   =   "/"
  49.       Style           =   6
  50.       Appearance      =   1
  51.    End
  52.    Begin VB.Label Label1 
  53.       BorderStyle     =   1  'Fixed Single
  54.       BeginProperty Font 
  55.          Name            =   "Verdana"
  56.          Size            =   9
  57.          Charset         =   0
  58.          Weight          =   400
  59.          Underline       =   0   'False
  60.          Italic          =   0   'False
  61.          Strikethrough   =   0   'False
  62.       EndProperty
  63.       Height          =   300
  64.       Left            =   3630
  65.       TabIndex        =   2
  66.       Top             =   4665
  67.       Width           =   5550
  68.    End
  69. Attribute VB_Name = "ExplorerForm"
  70. Attribute VB_GlobalNameSpace = False
  71. Attribute VB_Creatable = False
  72. Attribute VB_PredeclaredId = True
  73. Attribute VB_Exposed = False
  74. '  ******************************
  75. '  ******************************
  76. '  ** MASTERING VB6            **
  77. '  ** by Evangelos Petroutos   **
  78. '  ** SYBEX, 1998              **
  79. '  ******************************
  80. '  ******************************
  81. Dim FSys As FileSystemObject
  82. Dim TimerBusy As Boolean
  83. Sub ScanFolder(folderSpec)
  84. Dim thisFolder As Folder
  85. Dim allFolders As Folders
  86.     Set thisFolder = FSys.GetFolder(folderSpec)
  87.     Set allFolders = thisFolder.SubFolders
  88.     For Each thisFolder In allFolders
  89.         TreeView1.Nodes.Add UCase(thisFolder.ParentFolder.Path), tvwChild, UCase(thisFolder.Path), thisFolder.Name
  90.         ScanFolder (thisFolder.Path)
  91.     Next
  92. End Sub
  93. Private Sub Form_Load()
  94.     LWidth = ListView1.Width - 5 * Screen.TwipsPerPixelX
  95.     ListView1.ColumnHeaders.Add 1, , "File Name", 0.3 * LWidth
  96.     ListView1.ColumnHeaders.Add 2, , "Size", 0.2 * LWidth, lvwColumnRight
  97.     ListView1.ColumnHeaders.Add 3, , "Created", 0.25 * LWidth
  98.     ListView1.ColumnHeaders.Add 4, , "Modified", 0.25 * LWidth
  99.     Set FSys = CreateObject("Scripting.FileSystemObject")
  100. ' Specify the folder you wish to map in the following line
  101.     InitPath = "C:\WINDOWS"
  102.     TreeView1.Nodes.Add , tvwFirst, UCase(InitPath), InitPath
  103.     Me.Show
  104.     Screen.MousePointer = vbHourglass
  105.     DoEvents
  106.     ScanFolder (InitPath)
  107.     Screen.MousePointer = vbDefault
  108. End Sub
  109. Private Sub ListView1_ColumnClick(ByVal ColumnHeader As ComctlLib.ColumnHeader)
  110.     ListView1.SortKey = ColumnHeader.Index - 1
  111.     ' Set Sorted to True to sort the list.
  112.     ListView1.Sorted = True
  113. End Sub
  114. Private Sub Timer1_Timer()
  115. '   If this event handler is executing, don't start it again
  116.     If TimerBusy Then Exit Sub
  117. '   Timerbusy indicates that the subroutine is calculating
  118.     TimerBusy = True
  119.     Dim totFiles As Integer, totSize As Long
  120.     totFiles = 0
  121.     totSize = 0
  122.     On Error Resume Next
  123. '   scan all files in the ListView control and examine
  124. '   their Selected property. If it's True, then increase the
  125. '   count of selected files (totFiles) and add its size to the total (TotSize)
  126.     For i = 1 To ListView1.ListItems.Count
  127.         If ListView1.ListItems(i).Selected Then
  128.             totFiles = totFiles + 1
  129.             totSize = totSize + ListView1.ListItems(i).SubItems(1)
  130.         End If
  131.     Next
  132. '   Display the file count and their total size
  133.     Label1.Caption = Format(totFiles, "###,##0") & " files selected, containing " & Format(totSize, "###,###,###,##0") & " bytes"
  134. '   and reset the TimerBusy variable, so that it can be invoked again
  135.     TimerBusy = False
  136. End Sub
  137. Private Sub TreeView1_NodeClick(ByVal Node As ComctlLib.Node)
  138. ' When a folder is selected on the TreeView control, its files
  139. ' must be displayed in the ListView control.
  140. ' To display the files and their attributes, it scans the
  141. ' members of the allFiles collection, which contains all the files
  142. ' in the selected folder
  143. Dim thisFolder As Folder
  144. Dim thisFile As File
  145. Dim allFiles As Files
  146. Dim thisItem  As ListItem
  147.     Screen.MousePointer = vbHourglass
  148.     ListView1.ListItems.Clear
  149. '   Create a Folder variable that references the selected folder
  150.     Set thisFolder = FSys.GetFolder(Node.Key)
  151. '   and use its Files property to retrieve the folder's files
  152.     Set allFiles = thisFolder.Files
  153.     If allFiles.Count > 0 Then
  154. On Error Resume Next
  155. '   Now scan all the files in the allFiles collection
  156. '   Use the properties of the variable thisItem to retrieve the file attributes
  157. '   and attach them to the ListView control as subitems of the current file
  158.         For Each thisFile In allFiles
  159.                 Set thisItem = ListView1.ListItems.Add(, , thisFile.Name)
  160.                 thisItem.SubItems(1) = Format(thisFile.Size, "###,###,###")
  161.                 thisItem.SubItems(2) = Left(thisFile.DateCreated, 8)
  162.                 thisItem.SubItems(3) = Left(thisFile.DateLastModified, 8)
  163.                 If thisFile.Attributes And vbSystem Then thisItem.Ghosted = True
  164.         Next
  165.     End If
  166.     Screen.MousePointer = vbDefault
  167. End Sub
  168.