[RESOLVED] ExplorerTreeView
I have made an ExplorerTreeView and it works great except for when I single click on a file the icon changes from its icon to the open folder icon and when I click on a different file it changes back! Here is my code that I was gonna share here if anyone can help ... And also when I double click a folder it opens in Windows I only want it to expand in treeview
Code:
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Public Class DexTreeView
Private sysIcons As New SystemIconsImageList()
Private mRootPath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Property RootPath As String
Get
Return mRootPath
End Get
Set(value As String)
mRootPath = value
End Set
End Property
Private Sub DexTreeView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' when our component is loaded, we initialize the TreeView by adding the root node
Dim mRootNode As New TreeNode
mRootNode.Text = RootPath
mRootNode.Tag = RootPath
mRootNode.Nodes.Add("*DUMMY*")
TreeView2.Nodes.Add(mRootNode)
TreeView2.ImageList = sysIcons.SmallIconsImageList
TreeView2.Nodes(0).Expand()
End Sub
Private Sub TreeView2_BeforeCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView2.BeforeCollapse
' clear the node that is being collapsed
e.Node.Nodes.Clear()
' add a dummy TreeNode to the node being collapsed so it is expandable
e.Node.Nodes.Add("*DUMMY*")
End Sub
Private Sub TreeView2_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView2.BeforeExpand
' clear the expanding node so we can re-populate it, or else we end up with duplicate nodes
e.Node.Nodes.Clear()
' get the directory representing this node
Dim mNodeDirectory As IO.DirectoryInfo
mNodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
' add each subdirectory from the file system to the expanding node as a child node
For Each mDirectory As IO.DirectoryInfo In mNodeDirectory.GetDirectories
' declare a child TreeNode for the next subdirectory
Dim mDirectoryNode As New TreeNode
' store the full path to this directory in the child TreeNode's Tag property
mDirectoryNode.Tag = mDirectory.FullName
' set the child TreeNodes's display text
mDirectoryNode.Text = mDirectory.Name
' add a dummy TreeNode to this child TreeNode to make it expandable
mDirectoryNode.Nodes.Add("*DUMMY*")
' add this child TreeNode to the expanding TreeNode
e.Node.Nodes.Add(mDirectoryNode)
mDirectoryNode.ImageIndex = sysIcons.GetIconIndex(mDirectoryNode.Tag)
Next
' add each file from the file system that is a child of the argNode that was passed in
For Each mFile As IO.FileInfo In mNodeDirectory.GetFiles
' declare a TreeNode for this file
Dim mFileNode As New TreeNode
' store the full path to this file in the file TreeNode's Tag property
mFileNode.Tag = mFile.FullName
' set the file TreeNodes's display text
mFileNode.Text = mFile.Name
' add this file TreeNode to the TreeNode that is being populated
e.Node.Nodes.Add(mFileNode)
mFileNode.ImageIndex = sysIcons.GetIconIndex(mFileNode.Tag)
Next
End Sub
Private Sub TreeView2_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView2.NodeMouseDoubleClick
' only proceed if the node represents a file
If e.Node.ImageKey = "folder" Then Exit Sub
If e.Node.Tag = "" Then Exit Sub
' try to open the file
Try
Process.Start(e.Node.Tag)
Catch ex As Exception
MessageBox.Show("Error opening file: " & ex.Message)
End Try
End Sub
End Class