Results 1 to 10 of 10

Thread: [RESOLVED] ExplorerTreeView

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    43

    Resolved [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
    I may be new,but I've been that way for a long time!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: ExplorerTreeView

    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!

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    43

    Re: ExplorerTreeView

    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!

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: ExplorerTreeView

    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!

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    43

    Re: ExplorerTreeView

    Thanks for looking Im beating my brains out over these two problems ... Here's the winrar file, sorry but PM would'nt let me add attachments
    Attached Files Attached Files
    I may be new,but I've been that way for a long time!

  6. #6

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    43

    Re: ExplorerTreeView

    Sorry it did'nt attach first time the board really slow this morning
    Attached Files Attached Files
    I may be new,but I've been that way for a long time!

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    43

    Re: ExplorerTreeView

    Well I guess it did LOL ...
    I may be new,but I've been that way for a long time!

  8. #8
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: ExplorerTreeView

    Whenever you have a Control that isn't behaving as you expect it to, you should always check the documents for that Control.

    If you look at the documents for the TreeView Control and scroll right on down to the Remarks section you will see that it has a SelectedImageIndex property as well as an ImageIndex property (not to mention the ImageKey and SelectedImageKey properties).

    Note that the same is also true for TreeNodes.


    If you read up on those properties you will see that (from http://msdn.microsoft.com/en-us/libr...vs.100%29.aspx )
    Quote Originally Posted by MSDN
    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:
    1. Private Sub TreeView2_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView2.NodeMouseDoubleClick
    2.     ' only proceed if the node represents a file
    3.     Try
    4.         If File.Exists(e.Node.Tag.ToString) Then
    5.             Process.Start(e.Node.Tag.ToString)
    6.         End If
    7.     Catch ex As Exception
    8.         MessageBox.Show("Error opening file: " & ex.Message)
    9.     End Try
    10. End Sub


    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.

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    43

    Re: ExplorerTreeView

    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!

  10. #10

    Thread Starter
    Member
    Join Date
    Jul 2012
    Posts
    43

    Cool Re: ExplorerTreeView

    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!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width