hood8jmark
New member
- Joined
- Mar 27, 2008
- Messages
- 2
- Programming Experience
- 3-5
Hi All,
This may be a very simple solution but as I am so new to VB.NET as opposed to VB6, I'm gonna need some help. How can I capture the the folder path of a selected node in a treeview? I have a treeview set up to be populated with with folders based on my systems directory structure. I need to be able to select the target folder (not when expand the node but when I select it) and return the folder path. I'm using the code below to accomplish this. Any help would be greatly appreciated. Thanks.

This may be a very simple solution but as I am so new to VB.NET as opposed to VB6, I'm gonna need some help. How can I capture the the folder path of a selected node in a treeview? I have a treeview set up to be populated with with folders based on my systems directory structure. I need to be able to select the target folder (not when expand the node but when I select it) and return the folder path. I'm using the code below to accomplish this. Any help would be greatly appreciated. Thanks.
VB.NET:
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Archiver
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
_
Public szDisplayName As String
_
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private Const MAX_PATH = 260
Private Sub AddImages(ByVal strFileName As String)
Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As Drawing.Bitmap
MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
ImageList1.Images.Add(strFileName.ToString(), MyIcon)
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("Loading...")
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Archiver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Tnode As TreeNode = TreeView1.Nodes.Add("(Drive S:)")
AddAllFolders(Tnode, "S:\")
ListView1.View = View.Details
' Add a column with width 80 and left alignment
ListView1.Columns.Add("File Name", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("File Type", 80, HorizontalAlignment.Left)
ListView1.Columns.Add("Date Modified", 150, HorizontalAlignment.Left)
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim FileExtension As String
Dim SubItemIndex As Integer
Dim DateMod As String
ListView1.Items.Clear()
If TreeView1.SelectedNode.Nodes.Count = 1 AndAlso TreeView1.SelectedNode.Nodes(0).Text = "Loading..." Then
TreeView1.SelectedNode.Nodes.Clear()
AddAllFolders(TreeView1.SelectedNode, CStr(TreeView1.SelectedNode.Tag))
End If
Dim folder As String = CStr(TreeView1.SelectedNode.Tag)
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder)
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
SubItemIndex += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then
e.Node.Nodes.Clear()
AddAllFolders(e.Node, CStr(e.Node.Tag))
End If
End Sub
Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
End Sub
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Private Sub TreeView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.Click
End Sub
Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
'Dim fldr As String
'fldr = TreeView1.SelectedNode.FullPath
'MsgBox(fldr)
End Sub
End Class