Load Tree Subnodes on Demand

Load Tree Subnodes on Demand

Here’s a quick way to drop a drive’s folder hierarchy into a TreeView control. The advantage to using this method is that folders are enumerated only when a node is expanded, so your app won’t waste time adding folders the user will never look at. A dummy item is added to each folder to get a plus sign on the node, but the item is removed when the node is expanded or simply disappears if the folder has no subdirectories. This code assumes your TreeView is named tvFiles and you have a button named cmdEnum on the form. The TreeView is connected to an ImageList whose first image is a folder icon:

 Private Type WIN32_FIND_DATA	dwFileAttributes As Long	ftCreationTime As Currency 'FILETIME	ftLastAccessTime As Currency 'FILETIME	ftLastWriteTime As Currency 'FILETIME	nFileSizeHigh As Long	nFileSizeLow As Long	dwReserved0 As Long	dwReserved1 As Long	cFileName As String * 260	cAlternate As String * 14End TypePrivate Const FILE_ATTRIBUTE_DIRECTORY = &H10Private Declare Function FindFirstFile Lib _	"kernel32" Alias "FindFirstFileA" _	(ByVal lpFileName As String, lpFindFileData _	As WIN32_FIND_DATA) As LongPrivate Declare Function FindNextFile Lib _	"kernel32" Alias "FindNextFileA" _	(ByVal hFindFile As Long, lpFindFileData _	As WIN32_FIND_DATA) As LongPrivate Declare Function FindClose Lib _	"kernel32" (ByVal hFindFile As Long) As LongPrivate Sub EnumFilesUnder(n As Node)	Dim hFind As Long	Dim sPath As String, oldPath As String	Dim wf As WIN32_FIND_DATA	Dim n2 As Node	sPath = n.FullPath & "*.*"	hFind = FindFirstFile(sPath, wf)	Do		' Get the filename, if any.		If InStr(wf.cFileName, vbNullChar) > 0 Then			sPath = Left$(wf.cFileName, _				InStr(wf.cFileName, vbNullChar) - 1)		Else			sPath = wf.cFileName		End If		If Len(sPath) = 0 Or StrComp(sPath, _			oldPath) = 0 Then			' Nothing found?			Exit Do		ElseIf sPath <> "." And sPath <> ".." Then			' Add file with folder image			If (wf.dwFileAttributes And _				FILE_ATTRIBUTE_DIRECTORY) _				= FILE_ATTRIBUTE_DIRECTORY Then				Set n2 = tvFiles.Nodes.Add(n, _					tvwChild, , wf.cFileName, 1, 1)				' Add a dummy item so the + sign is 				' displayed				tvFiles.Nodes.Add n2, tvwChild				n2.Sorted = True			End If		End If		FindNextFile hFind, wf		oldPath = sPath	Loop	FindClose hFindEnd SubPrivate Sub cmdEnum_Click()	tvFiles.Nodes.Clear	' Add an "initializing" item	tvFiles.Nodes.Add , , , "C:", 1	tvFiles.Nodes(1).Sorted = True	EnumFilesUnder tvFiles.Nodes.Item(1)	' Make sure the root is expanded	tvFiles.Nodes(1).Expanded = TrueEnd SubPrivate Sub tvFiles_Expand(ByVal Node _	As ComctlLib.Node)	If Node.Children = 1 Then		' Remove the "dummy" item		tvFiles.Nodes.Remove Node.Child.Index		' Enumerate file system items under 		' this node		EnumFilesUnder Node	End IfEnd Sub
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes