|
-
Nov 14th, 2009, 07:56 PM
#1
Thread Starter
Member
[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
-
Nov 14th, 2009, 08:12 PM
#2
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)
-
Nov 14th, 2009, 08:29 PM
#3
Thread Starter
Member
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
-
Nov 15th, 2009, 04:17 AM
#4
Thread Starter
Member
Re: How to have the path of a directory from a treeView
 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 ?
-
Nov 15th, 2009, 06:35 AM
#5
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
-
Nov 15th, 2009, 07:09 AM
#6
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
-
Nov 15th, 2009, 07:11 AM
#7
Re: How to have the path of a directory from a treeView
 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 
This way is far better than mine
-
Nov 15th, 2009, 07:12 AM
#8
Thread Starter
Member
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 !!!
-
Nov 15th, 2009, 07:12 AM
#9
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
-
Nov 15th, 2009, 07:14 AM
#10
Thread Starter
Member
Re: How to have the path of a directory from a treeView
thank you guys for everything !!!
the thread is resolved
-
Nov 15th, 2009, 07:15 AM
#11
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...
-
Nov 15th, 2009, 07:43 AM
#12
Thread Starter
Member
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.
-
Nov 15th, 2009, 07:52 AM
#13
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.
-
Nov 15th, 2009, 08:13 AM
#14
Thread Starter
Member
Re: [RESOLVED] How to have the path of a directory from a treeView
Alright , thats more clear now !
thank you
-
Nov 15th, 2009, 01:23 PM
#15
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
-
Nov 15th, 2009, 01:34 PM
#16
Thread Starter
Member
Re: How to have all the directories included in a treeview with main roots
That works just fine
Thank you
-
Nov 15th, 2009, 01:54 PM
#17
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.
-
Nov 15th, 2009, 04:17 PM
#18
Thread Starter
Member
Re: How to have all the directories included in a treeview with main roots
-
Nov 16th, 2009, 07:12 AM
#19
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|