Good evening. Starting from the top, until I expect a response from you. You will have to forgive if I change my approach. In this post, I will not go over your different naming standards that change with each same field etc. This is a clear insight to me you do not really understand all that is happening with the basics. We can expand on this later.
The main answer you seek is lag issues. If a UI control is lagging then to me instantly suggests that you are not adding each item in the correct manner causing the control to redraw each time.
The very first rule you should be following. Turn strict on always. How can we expect a program to run smoothly if we are writing shoddy code.
You are abusing the constructor. Public Sub New(). This is not for you to be doing work. Constructors should not do more than capture the constructor parameter. Specific user features should be simply that. User interaction. You also create a new instance of the ImageList class, then assign it to a default ImageList class that has no instance.
With that in mind. Let's see a rewritten example using the constructor correctly. Taking special note regarding RefreshFolders. Once you have posted a revised Constructor example only. We will move forward to the next step.
Microsoft has tried to move us way from the Registry for quite some time now.Please do not open a bigger can of worms for yourself.
Step gracefully through your project. It's scattered, unorganized and extremely hard to flow gracefully. I would like to see you think more about beautiful code. It's not just enough to write code to "as is".
A suggestion that might start getting you to think....
PHP Code
<?php
' TODO: [ESD] Put Option Strict at the top of all files.
' TODO: [ESD] Generate XML documentation; Types, members. Complete documentation being descriptive as possible.
' TODO: [ESD] (Task) Make sure that all exceptions are handled in a graceful manner.
' TODO: [ESD] (Task) Follow constructor rules. A default constructor of private where applicable.
' TODO: [ESD] (Bug) ....
' TODO: [ESD] (Behavior) ....
Imports System.IO
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class WindowsTreeView
Was hoping to see revised first part following the guidelines I laid out.
Something like...
Code
Option Strict On
' TODO: [ESD] Put Option Strict at the top of all files.
' TODO: [ESD] Generate XML documentation; Types, members. Complete documentation being descriptive as possible.
' TODO: [ESD] (Task) Make sure that all exceptions are handled in a graceful manner.
' TODO: [ESD] (Bug) ....
' TODO: [ESD] (Behavior) Remove all unwanted "DUMMY" nodes. This is not helping. It's noise.
Imports System.IO
Public Class Form1
' TODO: [ESD] (Bug) Focus on getting a working tree view as specified.
' TODO: [ESD] (Behavior) Code that is not used marked with a comment should be instantly removed.
Private ReadOnly mImageList As ImageList
' TODO: [ESD] (Task) Follow constructor rules. A default constructor of private where applicable.
' TODO: [ESD] (Task) All modifiers should be explicitly typed. You could walk into unknown bugs by not focusing on it's default type.
' TODO: [ESD] (Task) All Methods to be given meaningful names.
' TODO: [ESD] (Task) Remove all UI interactions.
' TODO: [ESD] (Task) Remove all icon calls temporary.
Private Function EnumerateFolderNodes(rootFolderPath As String) As TreeNode()
' TODO: [ESD] (Task) Using the correct variable scope.
' TODO: [ESD] (Task) Long running tasks are background work and anything to do with a control is inherently foreground work.
' TODO: [ESD] (Task) Provide a more efficient way to store data.
Dim nodes As New List(Of TreeNode)
Try
Dim rootFolder As New DirectoryInfo(rootFolderPath)
Dim folders = rootFolder.EnumerateDirectories()
For Each folder In folders
Dim folderPath = folder.FullName
Dim node As New TreeNode(folder.Name)
' TODO: [ESD] (Task) Handle recursive call's fore gracefully.
node.Nodes.AddRange(Me.EnumerateFolderNodes(folderPath))
nodes.Add(node)
Next
Catch ex As UnauthorizedAccessException
' TODO: [ESD] (Comment) This folder is inaccessible. All we can do is ignore and move on.
End Try
Return nodes.ToArray()
End Function
' TODO: [ESD] (Task) Me.EnumerateFolderNodes;
' : [ESD] My.Computer.FileSystem.SpecialDirectories
' : [ESD] Nodes.AddRange(nodes)
End Class