Results 1 to 3 of 3

Thread: VB ListView - ??????

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2000
    Posts
    5

    Unhappy

    Can anyone help me with the code to make a listview box display filenames from a directory in one column and file dates in another - I've tried but failed in vain!!!!

  2. #2
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    This is a program that i was messing about with a while ago, but i never got around to doing anything with it. Mainly because the File System Object is rather slow. I might get around to doing it with API one day.

    Anyway, it will show you the basics of filling a list box.

    You will need :

    ListView
    TreeView
    StatusBar


    Have Fun.

    Code:
    Option Explicit
    
    Dim fso           'the file system object
    Dim myFolder
    Dim myFile
    
    Dim iNumFiles As Long
    Dim iSize As Long
    
    
    
    Private Sub getFolderNames(folderCol)
        Dim mf
        Dim mf2
        Dim i As Integer
        Dim mykey As String, mytext As String, myparent As String
        
        For Each myFolder In folderCol.SubFolders
          mykey = UCase$(myFolder.Path) 'the key for the treeview
          mytext = myFolder.Name  'the text for the treeview
          myparent = UCase$(myFolder.ParentFolder)  ''the parent folder / realative in treeview
          
          'add the folder to the treeview
          TreeView1.Nodes.Add myparent, tvwChild, mykey, mytext, 1
          
          Set mf = myFolder.SubFolders
          i = 0
          'if the folder has any sub folders then add the first folder
          'to the treeview to make sure there is + next to the node
          For Each mf2 In mf
            i = i + 1
            mykey = UCase$(mf2.Path)
            mytext = mf2.Name
            myparent = UCase$(mf2.ParentFolder)
            TreeView1.Nodes.Add myparent, tvwChild, mykey, mytext, 1
            If i <> 0 Then Exit For
          Next
        Next
       
    End Sub
    
    Private Sub getFileNames(myFolder, myFoldersCol)
        
        Dim mykey As String, mytext As String
        
        Me.MousePointer = vbHourglass
        
        iNumFiles = 0
        iSize = 0
        
        For Each myFile In myFoldersCol
          'loop to display the sub-folders in the folders-collection
          'in the listview
          mykey = myFile.Path
          mytext = myFile.Name
          ListView1.ListItems.Add , mykey, mytext, 1, 1
        Next
        
        For Each myFile In myFolder
          'loop through each file in the folder to add it to
          'the list view
          mykey = UCase$(myFile.Path)
          mytext = myFile.Name
          
          ListView1.ListItems.Add , mykey, mytext  'add the file name
          ListView1.ListItems.Item(mykey).ListSubItems.Add , mykey, Format$(myFile.Size, "#,##0")  'add the file size
          ListView1.ListItems.Item(mykey).ListSubItems.Add , , myFile.Type  'add the file type
          ListView1.ListItems.Item(mykey).ListSubItems.Add , , myFile.DateLastModified   'add the date modified
          ListView1.ListItems.Item(mykey).ListSubItems.Add , , myFile.Attributes   'add the attributes of the file
          
          iNumFiles = iNumFiles + 1   'count the number of files
          iSize = iSize + myFile.Size   'count the file sizes
        Next
        
        StatusBar1.Panels(1).Text = iNumFiles & "  Object(s)"
        If iSize > (CLng(1024) * 1024) Then
          StatusBar1.Panels(2).Text = Format$(iSize / 1024 / 1024, "##.##") & "MB"
        ElseIf iSize > 1024 Then
          StatusBar1.Panels(2).Text = Format$(iSize / 1024, "##.##") & "KB"
        Else
          StatusBar1.Panels(2).Text = iSize & " Bytes"
        End If
        
        Me.MousePointer = vbDefault
    
    End Sub
    
    
    
    
    Private Sub Form_Load()
        Dim strFolder
            
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        strFolder = "C:\"
        
        TreeView1.Nodes.Add , , UCase$(strFolder), UCase$(strFolder), 1
        getFileNames fso.GetFolder(strFolder).Files, fso.GetFolder(strFolder).SubFolders
        getFolderNames (fso.GetFolder(strFolder))
    
    End Sub
    
    
    Private Sub TreeView1_Collapse(ByVal Node As MSComctlLib.Node)
        Dim myNode As Node
        Dim i As Integer
        
    
        Node.Image = 1
        If Node.Key <> "C:\" Then
          For i = 1 To Node.Children - 1
            TreeView1.Nodes.Remove (Node.Child.Key)
          Next i
        End If
        
    End Sub
    
    
    Private Sub TreeView1_Expand(ByVal Node As MSComctlLib.Node)
        
        Node.Image = 2
    
        
        If Node.Key <> "C:\" Then
          TreeView1.Nodes.Remove (Node.Child.Key)
          getFolderNames (fso.GetFolder(Node.Key))
        End If
        
    End Sub
    
    Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
        
        ListView1.ListItems.Clear
        getFileNames fso.GetFolder(Node.Key).Files, fso.GetFolder(Node.Key).SubFolders
        Me.Caption = "Exploring - " & Node.Key
        
    End Sub
    
    Private Sub Form_Resize()
        If Me.Height > 900 And _
           Me.Width > 3000 _
        Then
          TreeView1.Move 0, 100, 3000, Me.Height - 900
          ListView1.Move TreeView1.Width, 100, Me.Width - TreeView1.Width - 100, Me.Height - 900
          StatusBar1.Panels(2).Width = Me.Width - StatusBar1.Panels(1).Width
        End If
    End Sub
    Iain, thats with an i by the way!

  3. #3
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Oh yeah i forgot.

    You need to set the "View" property of the ListView to "lvwReport"

    This can be done at design or run time.
    Iain, thats with an i by the way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width