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
I may be new,but I've been that way for a long time!
Is this your complete code? There doesn't actually appear to be a NodeMouseClick sub which is gonna make analysing it kinda difficult!
Also your DoubleClick sub manifestly uses Process.Start so I'm not sure what else you would expect other than to open the file.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
Sorry dunfiddlin I had this on back burner because it was'nt working. I keep getting "The diretory name is invalid!" And for the other I guess if anyone had the code so only files would open and not folders!
Code:
Private Sub TreeView2_NodeMouseClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView2.NodeMouseClick
' 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
I may be new,but I've been that way for a long time!
Don't think I'm gonna get a handle on this from bits of code alone. If you'd like to PM with your project all zipped up for me to look at in acyion I'll be happy to do that for you.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
The ImageKey and ImageIndex properties are mutually exclusive; therefore, if one property is set, the other is ignored. If you set the ImageKey property, the ImageIndex property is automatically set to -1. Alternatively, if you set ImageIndex, ImageKey is automatically set to an empty string ("").
so the line
Code:
If e.Node.ImageKey = "folder" Then Exit Sub
in your TreeView2_NodeMouseDoubleClick sub won't be working as expected since you use the ImageIndex property to assign your images to the Nodes.
This is the reason that the DoubleClick on a Node is opening its Folder. The above check is failing. You need a better way of determining if a Node is representing a Folder or a File.
One way might be to check if ImageIndex = 0 or even just check if the file exists before trying to run it.
vb.net Code:
Private Sub TreeView2_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView2.NodeMouseDoubleClick
As to your Click event handler code; from what you've said so far, it doesn't look like you actually need this, but only you know what else you might want your program to do. It seems to be the exact same code as your TreeView2_BeforeExpand handler code. That code creates nodes for displaying the contents of a Folder. It gets info about the Folder's contents by creating an instance of the DirectoryInfo Class for the Folder. However, in the Click event handler code you are not differentiating between clicking on a File or clicking on a Folder. The code tries to create a DirectoryInfo instance for the File represented by the Node you click on, which clearly can't work. Check if the Node clicked upon represents a File or a Folder and don't execute the code if it's a File.
GREAT job Inferrd that worked on the double click and Im going to use your advice on the click. I have read about the ImageIndex and Imagekey before and could'nt figure it out but will keep trying and post results here. Thanx again
I may be new,but I've been that way for a long time!
I have figure it out and believe it was that simple thanks to Inferrd! Here is my modification and it was only one line.
Before:
Code:
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
After:
Code:
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)
mFileNode.SelectedImageIndex = sysIcons.GetIconIndex(mFileNode.Tag)
Next
End Sub
Now I need to work on speeding it up! Thanks again Inferrd :)
I may be new,but I've been that way for a long time!