Need help to show me the size of selected item in listbox (treeview)

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, I need help to show me the size of selected item in listbox (treeview). Blue hilighted area is where i exactly need help.
From the search results as i click on list item it shows me properties of the item. I want to add size to it.

:)link!

this below code is from cpearsons blog.
Code:
[/FONT]
[FONT=Courier New][SIZE=1]Option Explicit[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Private Sub btnClose_Click()
    Unload Me
End Sub[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Private Sub btnLoadTree_Click()
''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This is the Click event of a button on the UserForm.
' It sets some options and then calls the procedure
' CreateFolderTreeView which actually builds the
' tree.
''''''''''''''''''''''''''''''''''''''''''''''''''''''[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Dim TVX As MSComctlLib.TreeView
Dim TopFolderName As String
Dim ShowFullPaths As Boolean
Dim ShowFiles As Boolean
Dim ItemDescriptionToTag  As Boolean[/SIZE][/FONT]

[FONT=Courier New][SIZE=1]'''''''''''''''''''''''''''''''''''''''''''''''''''
' Options
'''''''''''''''''''''''''''''''''''''''''''''''''''[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]''''''''''''''''''''''''''''''''''''''
' If ShowFullPaths is True, the
' fully qualified folder and file
' names will appear in the tree. If
' ShowFullpaths is False, only the
' name of the folder or file is
' displayed in the tree.
''''''''''''''''''''''''''''''''''''''
ShowFullPaths = False[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]''''''''''''''''''''''''''''''''''''''
' If ShowFiles is True, files are listed
' below the folder in which they reside.
' If ShowFiles is False, files are not
' listed in the tree and only folders
' are listed.
''''''''''''''''''''''''''''''''''''''
ShowFiles = True
''''''''''''''''''''''''''''''''''''''
' If ItemDescriptionToTag is True, the
' Tag property of each node will contain
' the word "FOLDER" or "FILE" followed
' by vbCrLf followed by the fully qualified
' name of the folder or file. This information
' can be used to identify the folder or file
' to which the node refers.
''''''''''''''''''''''''''''''''''''''''
ItemDescriptionToTag = True[/SIZE][/FONT]

[FONT=Courier New][SIZE=1]' <<< BEGIN BrowseFolder Code
'''''''''''''''''''''''''''''''''''''''''''''''''''
' This example uses the BrowseFolder function
' available at
' [/SIZE][/FONT][URL="http://www.cpearson.com/Excel/BrowseFolder.aspx"][FONT=Courier New][SIZE=1]http://www.cpearson.com/Excel/BrowseFolder.aspx[/SIZE][/FONT][/URL]
[FONT=Courier New][SIZE=1]' to allow the user to select the top level folder.
' In your code, you can assign an explicit folder
' name to the variable TopFolderName or retrieve
' the folder name using whatever mechanism you want.
' However you do it, TopFolderName must be set
' to the name of an existing folder.
'''''''''''''''''''''''''''''''''''''''''''''''''''
TopFolderName = BrowseFolder("Select A Folder For The TreeView")
If TopFolderName = vbNullString Then
    MsgBox "Operation Cancelled", vbOKOnly, Me.Caption
    Exit Sub
End If
' >>> END BrowseFolder Code[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]'''''''''''''''''''''''''''
' Ensure the folder exists.
'''''''''''''''''''''''''''
If Dir(TopFolderName, vbDirectory) = vbNullString Then
    MsgBox "Folder: '" & TopFolderName & "' does not exist.", vbOKOnly, Me.Caption
    Exit Sub
End If[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]'''''''''''''''''''''''''''
' Create the actual tree
' nodes.
'''''''''''''''''''''''''''
CreateFolderTreeView TVX:=Me.TreeView1, _
                    TopFolderName:=TopFolderName, _
                    ShowFullPaths:=ShowFullPaths, _
                    ShowFiles:=ShowFiles, _
                    ItemDescriptionToTag:=ItemDescriptionToTag[/SIZE][/FONT]

[FONT=Courier New][SIZE=1]End Sub[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][COLOR=blue]Private Sub btnNodeInfo_Click()
''''''''''''''''''''''''''''''''''''''''''''''''''
' This displays information about the node.
''''''''''''''''''''''''''''''''''''''''''''''''''[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=1][COLOR=blue]Dim Arr As Variant
Dim SelectedNode As MSComctlLib.Node
Dim S As String[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=1][COLOR=blue]Set SelectedNode = Me.TreeView1.SelectedItem[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=1][COLOR=blue]If SelectedNode Is Nothing Then
    MsgBox "No Node Selected", vbOKOnly, Me.Caption
    Exit Sub
End If[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=1][COLOR=blue]With SelectedNode
    S = .Text & vbCrLf
    If Len(.Tag) > 0 Then
        Arr = Split(.Tag, vbCrLf)
        S = S & "Item Is: " & Arr(LBound(Arr)) & vbCrLf
        S = S & "Refers To: " & Arr(LBound(Arr) + 1) & vbCrLf
        S = S & "Count Of Children: " & CStr(.Children) & vbCrLf
    Else
        S = .Text & vbCrLf
        S = S & "Count Of Children: " & CStr(.Children)
    End If
    MsgBox S, vbOKOnly, .Text
End With[/COLOR][/SIZE][/FONT]

[FONT=Courier New][SIZE=1][COLOR=blue]End Sub[/COLOR][/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Public Sub CreateFolderTreeView(TVX As MSComctlLib.TreeView, _
                            TopFolderName As String, _
                            ShowFullPaths As Boolean, _
                            ShowFiles As Boolean, _
                            ItemDescriptionToTag As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This procedure loads the TreeView with file and folder
' elements.
' Parameters:
' ------------
' TVX: The TreeView control that will be loaded.
'
' TopFolderName: The fully-qualified folder name
' whose contents are to be listed.
'
' ShowFullPaths: If True, the items in the tree will
' display the fully qualified folder or file name. If
' False, only the name, with not path information, will
' be displayed.
'
' ShowFiles: If True, files within a folder will be listed.
' If False, only folders, not files, will appear in the
' Tree listing.
'
' ItemDescriptionToTag: If True, information about the
' file or folder is stored in the Tag property of the
' Node. This information is either the word "FOLDER"
' or "FILE" followed by a vbCrLf followed by the fully
' qualified name of the folder or file.
'
' This code can reside in a standard code module - it
' need not be in the UserForm's code module.
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Dim TopFolder As Scripting.Folder
Dim OneFile As Scripting.File
Dim FSO As Scripting.FileSystemObject
Dim TopNode As MSComctlLib.Node
Dim S As String
Dim FileNode As MSComctlLib.Node[/SIZE][/FONT]

[FONT=Courier New][SIZE=1]Set FSO = New Scripting.FileSystemObject
'''''''''''''''''''''''
' Clear the tree
'''''''''''''''''''''''
TVX.Nodes.Clear[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Set TopFolder = FSO.GetFolder(folderpath:=TopFolderName)[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]'''''''''''''''''''''''''''''''''''
' Create the top node of the
' TreeView.
'''''''''''''''''''''''''''''''''''
If ShowFullPaths = True Then
    S = TopFolder.Path
Else
    S = TopFolder.Name
End If
    
Set TopNode = TVX.Nodes.Add(Text:=S)[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]If ItemDescriptionToTag = True Then
    TopNode.Tag = "FOLDER" & vbCrLf & TopFolder.Path
End If[/SIZE][/FONT]

[FONT=Courier New][SIZE=1]''''''''''''''''''''''''''''''''''''''''''
' Call CreateNodes. This procedure creates
' all the nodes of the tree using a
' recursive technique -- that is, the code
' calls itself to create child nodes, child
' of child nodes, and so on.
''''''''''''''''''''''''''''''''''''''''''
CreateNodes TVX:=TVX, _
            FSO:=FSO, _
            ParentNode:=TopNode, _
            FolderObject:=TopFolder, _
            ShowFullPaths:=ShowFullPaths, _
            ShowFiles:=ShowFiles, _
            ItemDescriptionToTag:=ItemDescriptionToTag[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]'''''''''''''''''''''''''''''''''''''''''
' After all the subfolders are built,
' we need to add the folders that
' are directly below the TopFolder.
'''''''''''''''''''''''''''''''''''''''''
If ShowFiles = True Then
    For Each OneFile In TopFolder.Files
        If ShowFullPaths = True Then
            S = OneFile.Path
        Else
            S = OneFile.Name
        End If
        Set FileNode = TVX.Nodes.Add(relative:=TopNode, relationship:=tvwChild, Text:=S)
        If ItemDescriptionToTag = True Then
            FileNode.Tag = "FILE" & vbCrLf & OneFile.Path
        End If
    Next OneFile
End If[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]''''''''''''''''''''''''''''''''''''''''
' Finally, now that everything has
' been added to the TreeView, expand
' the top node.
'''''''''''''''''''''''''''''''''''''''
With TVX.Nodes
    If .Count > 0 Then
        .Item(1).Expanded = True
    End If
End With
    
End Sub[/SIZE][/FONT]

[FONT=Courier New][SIZE=1]Private Sub CreateNodes(TVX As MSComctlLib.TreeView, _
    FSO As Scripting.FileSystemObject, _
    ParentNode As MSComctlLib.Node, _
    FolderObject As Scripting.Folder, _
    ShowFullPaths As Boolean, _
    ShowFiles As Boolean, _
    ItemDescriptionToTag As Boolean)
''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This procedure creates the nodes on the Tree. This
' procedure can reside in a standard code module --
' it need not reside in the UserForm's code module.
' This procedure uses a recursive technique to build
' child nodes. That is, it calls itself to build the
' children, children of children, and so on.
'
' Parameters:
' -----------
' TVX: The TreeView controls to which the nodes will
' be added.
'
' FSO: A FileSystemObject used to enumerate subfolders
' and files of FolderObject.
'
' ParentNode: The Node that will be the parent of
' all nodes created by the current iteration of this
' procedure.
'
' FolderObject: The Folder object whose contents we
' are going to list. The TreeView element for
' FolderObject has already been added to the tree.
'
' ShowFullPaths: If True, elements in the tree will
' display the fully-qualified folder or file name. If
' False, only the name of the folder or file will
' appear in the tree. No path information will be
' displayed.
'
' ShowFiles: If True, files within a folder are listed
' in the tree. If False, no files are listed and
' only Folders will appear in the tree.
'
' ItemDescriptionToTag: If True, information about the
' folder or file is stored in the Tag property of the
' node. The infomation is the word "FOLDER" or "FILE"
' followed by a vbCrLF followed by the fully qualified
' name of the folder or file. If False, no information
' is stored in the Tag property.
''''''''''''''''''''''''''''''''''''''''''''''''''''''[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Dim SubNode As MSComctlLib.Node
Dim FileNode As MSComctlLib.Node
Dim OneSubFolder As Scripting.Folder
Dim OneFile As Scripting.File
Dim S As String[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]''''''''''''''''''''''''''''''''''''''''''''''''''
' Process each folder directly under FolderObject.
''''''''''''''''''''''''''''''''''''''''''''''''''
For Each OneSubFolder In FolderObject.SubFolders
    If ShowFullPaths = True Then
        S = OneSubFolder.Path
    Else
        S = OneSubFolder.Name
    End If
    
    '''''''''''''''''''''''''''''''''''
    ' Add the node to the tree.
    '''''''''''''''''''''''''''''''''''
    Set SubNode = TVX.Nodes.Add(relative:=ParentNode, relationship:=tvwChild, Text:=S)
    If ItemDescriptionToTag = True Then
        SubNode.Tag = "FOLDER" & vbCrLf & OneSubFolder.Path
    End If
    
    ''''''''''''''''''''''''''''''''''''''''''''''
    ' Recursive code. CreateNodes calls itself
    ' to add sub nodes to the tree. This recursion
    ' creates the children, children of children,
    ' and so on.
    ''''''''''''''''''''''''''''''''''''''''''''''
    CreateNodes TVX:=TVX, _
                FSO:=FSO, _
                ParentNode:=SubNode, _
                FolderObject:=OneSubFolder, _
                ShowFullPaths:=ShowFullPaths, _
                ShowFiles:=ShowFiles, _
                ItemDescriptionToTag:=ItemDescriptionToTag[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]    '''''''''''''''''''''''''''''''''
    ' If ShowFiles is True, add nodes
    ' for all the files.
    '''''''''''''''''''''''''''''''''
    If ShowFiles = True Then
        For Each OneFile In OneSubFolder.Files
            If ShowFullPaths = True Then
                S = OneFile.Path
            Else
                S = OneFile.Name
            End If
            Set FileNode = TVX.Nodes.Add(relative:=SubNode, relationship:=tvwChild, Text:=S)
            If ItemDescriptionToTag = True Then
                FileNode.Tag = "FILE" & vbCrLf & OneFile.Path
            End If
        Next OneFile
    
    End If[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Next OneSubFolder
    
    
End Sub[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]
[/SIZE]
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Hi

Here's a modified btnNodeInfo_Click procedure which includes the size of the folder or file. It currently shows the folder or file size in KB. If you want MB then change the 1024 to 1048576.

Code:
Private Sub btnNodeInfo_Click()
''''''''''''''''''''''''''''''''''''''''''''''''''
' This displays information about the node.
''''''''''''''''''''''''''''''''''''''''''''''''''

Dim Arr As Variant
Dim SelectedNode As MSComctlLib.Node
Dim S As String
Dim lSizeInBytes As Long
Dim oFSObj As Scripting.FileSystemObject, oFSFolder As Scripting.Folder

Set SelectedNode = Me.TreeView1.SelectedItem

If SelectedNode Is Nothing Then
    MsgBox "No Node Selected", vbOKOnly, Me.Caption
    Exit Sub
End If

With SelectedNode
    S = .Text & vbCrLf
    If Len(.Tag) > 0 Then
        Arr = Split(.Tag, vbCrLf)
        S = S & "Item Is: " & Arr(LBound(Arr)) & vbCrLf
        S = S & "Refers To: " & Arr(LBound(Arr) + 1) & vbCrLf
        S = S & "Count Of Children: " & CStr(.Children) & vbCrLf
    
       
            
        If Arr(LBound(Arr)) = "FOLDER" Then
            
            Set oFSObj = CreateObject("Scripting.FilesystemObject")
            Set oFSFolder = oFSObj.GetFolder(Arr(LBound(Arr) + 1))
            lSizeInBytes = oFSFolder.Size
            
        Else
        
           lSizeInBytes = FileLen(Arr(LBound(Arr) + 1))
            
        End If
            
        S = S & "Size Is: " & Round(lSizeInBytes / 1024, 0) & " KB"
            
            
    Else
        S = .Text & vbCrLf
        S = S & "Count Of Children: " & CStr(.Children)
    End If
    MsgBox S, vbOKOnly, .Text
End With


End Sub

HTH
DK
 
Upvote 0

Forum statistics

Threads
1,223,276
Messages
6,171,140
Members
452,381
Latest member
Nova88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top