Results 1 to 19 of 19

Thread: [RESOLVED] How to have the path of a directory from a treeView

  1. #1

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Resolved [RESOLVED] How to have the path of a directory from a treeView

    Hi ,
    I am using this code to have all the subdirectories in c:\program files and i am listing them on a treeView

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click



    Dim di As DirectoryInfo = New DirectoryInfo("c:\program files")
    Dim dirs As DirectoryInfo() = di.GetDirectories()
    Dim diNext As DirectoryInfo
    For Each diNext In dirs

    Dim C As TreeNode = TreeView1.Nodes.Add(diNext.Name)
    Next


    End Sub


    my next step is once i doubleclick on a node in the treeview , i get on a msgbox the path of it"s directory
    Can someone help me doing that
    Thank you

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: How to have the path of a directory from a treeView

    When you create the TreeNode, assign the FullName property of your DirectoryInfo to its Tag property. You can then handle the NodeMouseDoubleClick event and get the Tag property value of the node that was double-clicked.

    By the way, you should NEVER hard-code a file or folder path. In this case, do this:
    vb.net Code:
    1. Dim di As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.ProgramFiles)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Re: How to have the path of a directory from a treeView

    thank you for your response , i am really new to vb programing
    how can i assign the fullname property to the tag property ,

    i guess that after that i should put

    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    MsgBox(e.ToString)
    End Sub

    is that right
    Thank you

  4. #4

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Unhappy Re: How to have the path of a directory from a treeView

    Quote Originally Posted by saadmechiche View Post
    thank you for your response , i am really new to vb programing
    how can i assign the fullname property to the tag property ,

    i guess that after that i should put

    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    MsgBox(e.ToString)
    End Sub

    is that right
    Thank you
    Please any suggestion ?

  5. #5
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How to have the path of a directory from a treeView

    I could be wrong, but I don't think there is a way to show the directories full path the way you think. I think the only way, is do something like this is:

    vb.net Code:
    1. Dim strDirectoryPath As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim di As DirectoryInfo = New DirectoryInfo(strDirectoryPath)
    5.         Dim dirs As DirectoryInfo() = di.GetDirectories()
    6.  
    7.         For Each diNext As DirectoryInfo In dirs
    8.             Dim C As TreeNode = TreeView1.Nodes.Add(diNext.Name)
    9.         Next
    10.  
    11.     End Sub
    12.  
    13.     Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
    14.         MessageBox.Show(strDirectoryPath & "\" & Me.TreeView1.SelectedNode.Text)
    15.  
    16.     End Sub

    But as I said, I could be wrong
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  6. #6
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to have the path of a directory from a treeView

    Try

    Code:
            Dim dirs As DirectoryInfo() = di.GetDirectories()
            Dim diNext As DirectoryInfo
            For Each diNext In dirs
                Dim C As TreeNode = TreeView1.Nodes.Add(diNext.Name)
                C.Tag = diNext.FullName
            Next

  7. #7
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How to have the path of a directory from a treeView

    Quote Originally Posted by keystone_paul View Post
    Try

    Code:
            Dim di As DirectoryInfo = New DirectoryInfo("c:\program files")
            Dim dirs As DirectoryInfo() = di.GetDirectories()
            Dim diNext As DirectoryInfo
            For Each diNext In dirs
                Dim C As TreeNode = TreeView1.Nodes.Add(diNext.Name)
                C.Tag = diNext.FullName
            Next
    Like I said, I could be wrong

    This way is far better than mine
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  8. #8

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Re: How to have the path of a directory from a treeView

    No you are not wrong ,
    that's working perfectly thank you very much !!!

  9. #9
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: How to have the path of a directory from a treeView

    And to retrieve the path when the user double clicks the node :

    Code:
    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    
            MessageBox.Show(CType(sender, TreeView).SelectedNode.Tag.ToString)
    
        End Sub
    Last edited by keystone_paul; Nov 15th, 2009 at 07:57 AM. Reason: amended incorrect conversion

  10. #10

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Re: How to have the path of a directory from a treeView

    thank you guys for everything !!!
    the thread is resolved

  11. #11
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: [RESOLVED] How to have the path of a directory from a treeView

    Yes, it works, but mine (apparently) is not the best way to do it...
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  12. #12

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Re: [RESOLVED] How to have the path of a directory from a treeView

    by the way , can someone explain me what the sender is in

    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick

    I didn't also understant this nest code :

    MessageBox.Show(CType(sender, TreeNode).Tag.ToString)

    when i compile it , it retrurns a mistake , once i doubleclick on a node

    Thank your for your help !
    Last edited by saadmechiche; Nov 15th, 2009 at 07:51 AM.

  13. #13
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: [RESOLVED] How to have the path of a directory from a treeView

    Sorry - I made a mistake there in my code - didn't actually run it in visual studio... it should be

    Code:
    Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    
            MessageBox.Show(CType(sender, TreeView).SelectedNode.Tag.ToString)
    
        End Sub
    Sender is the object which raised the event... in this instance it will be the treeview control which raised the event. You could just replace references to sender with Treeview1 in this instance because that event will only ever fire when TreeView1 raises the event, but it is possible to have event handlers working with multiple controls and without knowing which control sent the event you would be stuck!

    CType(sender, TreeView) converts sender (which is of type object) to the specific type TreeView. SelectedNode then retrieves the node that is being clicked on.

    The .Tag is just retrieving the tag property of the node, and as the tag property holds an object, ToString returns the string version of the tag (ie the text you set earlier)
    Last edited by keystone_paul; Nov 15th, 2009 at 07:56 AM.

  14. #14

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Re: [RESOLVED] How to have the path of a directory from a treeView

    Alright , thats more clear now !
    thank you

  15. #15
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How to have all the directories included in a treeview with main roots

    This code will scan through all of your drives and add them in a TreeView:

    vb.net Code:
    1. Option Strict On
    2.  
    3. Imports System.IO
    4.  
    5. Public Class frmTest
    6.  
    7.     Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
    8.  
    9.         For Each drive As String In Environment.GetLogicalDrives
    10.             Dim Tnode As TreeNode = treeView.Nodes.Add("(Drive " & drive & ")")
    11.             AddAllFolders(Tnode, drive)
    12.         Next
    13.  
    14.     End Sub
    15.  
    16.     Private Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
    17.         Try
    18.             For Each FolderNode As String In Directory.GetDirectories(FolderPath)
    19.                 Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))
    20.  
    21.                 SubFolderNode.Tag = FolderNode
    22.  
    23.                 SubFolderNode.Nodes.Add("{child}")
    24.             Next
    25.         Catch e As IOException
    26.             'Do nothing, since you will get an exception when adding an empty CD/DVD-drive...
    27.  
    28.         Catch ex As Exception
    29.             MessageBox.Show(ex.Message, "TreeView example", MessageBoxButtons.OK, MessageBoxIcon.Error)
    30.  
    31.         End Try
    32.     End Sub
    33.  
    34.     Private Sub treeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeView.AfterSelect
    35.  
    36.         If treeView.SelectedNode.Nodes.Count = 1 AndAlso treeView.SelectedNode.Nodes(0).Text = "{child}" Then
    37.             treeView.SelectedNode.Nodes.Clear()
    38.             AddAllFolders(treeView.SelectedNode, CStr(treeView.SelectedNode.Tag))
    39.         End If
    40.  
    41.     End Sub
    42.  
    43.     Private Sub treeView_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles treeView.BeforeExpand
    44.  
    45.         If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "{child}" Then
    46.             e.Node.Nodes.Clear()
    47.             AddAllFolders(e.Node, CStr(e.Node.Tag))
    48.         End If
    49.  
    50.     End Sub
    51.  
    52. End Class
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  16. #16

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Re: How to have all the directories included in a treeview with main roots

    That works just fine
    Thank you

  17. #17
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How to have all the directories included in a treeview with main roots

    Great Remember to mark your thread as resolved
    Last edited by Arve K.; Nov 15th, 2009 at 01:59 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  18. #18

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    60

    Re: How to have all the directories included in a treeview with main roots

    All right , done !

  19. #19
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] How to have the path of a directory from a treeView

    Duplicate threads merged.

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