[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
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:
Dim di As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.ProgramFiles)
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
Re: How to have the path of a directory from a treeView
Quote:
Originally Posted by
saadmechiche
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 ?
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:
Dim strDirectoryPath As String = My.Computer.FileSystem.SpecialDirectories.ProgramFiles
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim di As DirectoryInfo = New DirectoryInfo(strDirectoryPath)
Dim dirs As DirectoryInfo() = di.GetDirectories()
For Each diNext As DirectoryInfo In dirs
Dim C As TreeNode = TreeView1.Nodes.Add(diNext.Name)
Next
End Sub
Private Sub TreeView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DoubleClick
MessageBox.Show(strDirectoryPath & "\" & Me.TreeView1.SelectedNode.Text)
End Sub
But as I said, I could be wrong :)
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
Re: How to have the path of a directory from a treeView
Quote:
Originally Posted by
keystone_paul
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 :p
This way is far better than mine ;)
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 !!!
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
Re: How to have the path of a directory from a treeView
thank you guys for everything !!!
the thread is resolved
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... :)
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 !
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)
Re: [RESOLVED] How to have the path of a directory from a treeView
Alright , thats more clear now !
thank you
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:
Option Strict On
Imports System.IO
Public Class frmTest
Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
For Each drive As String In Environment.GetLogicalDrives
Dim Tnode As TreeNode = treeView.Nodes.Add("(Drive " & drive & ")")
AddAllFolders(Tnode, drive)
Next
End Sub
Private Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
Try
For Each FolderNode As String In Directory.GetDirectories(FolderPath)
Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))
SubFolderNode.Tag = FolderNode
SubFolderNode.Nodes.Add("{child}")
Next
Catch e As IOException
'Do nothing, since you will get an exception when adding an empty CD/DVD-drive...
Catch ex As Exception
MessageBox.Show(ex.Message, "TreeView example", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub treeView_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeView.AfterSelect
If treeView.SelectedNode.Nodes.Count = 1 AndAlso treeView.SelectedNode.Nodes(0).Text = "{child}" Then
treeView.SelectedNode.Nodes.Clear()
AddAllFolders(treeView.SelectedNode, CStr(treeView.SelectedNode.Tag))
End If
End Sub
Private Sub treeView_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles treeView.BeforeExpand
If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "{child}" Then
e.Node.Nodes.Clear()
AddAllFolders(e.Node, CStr(e.Node.Tag))
End If
End Sub
End Class
Re: How to have all the directories included in a treeview with main roots
That works just fine
Thank you
Re: How to have all the directories included in a treeview with main roots
Great:) Remember to mark your thread as resolved :)
Re: How to have all the directories included in a treeview with main roots
Re: [RESOLVED] How to have the path of a directory from a treeView
Duplicate threads merged.