PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Load files and subfolder with windows icons in treeview-VBForums
Results 1 to 1 of 1

Thread: Load files and subfolder with windows icons in treeview

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Load files and subfolder with windows icons in treeview

    Here's an example to show you how to load a particular folder and load all the files and subfolders with the original Windows icons. Next to that, the tag of the files and folders get the original path.



    See the attached example for all the controls used etc.

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2. Imports System.IO
    3.  
    4. Public Class Form1
    5.  
    6.     Dim rootNode, tNode, tempNode As TreeNode
    7.     Dim icoList As New List(Of Integer)
    8.  
    9. #Region "   ICON DECLARATIONS"
    10.  
    11.     Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
    12.             (ByVal pszPath As String, _
    13.              ByVal dwFileAttributes As Integer, _
    14.              ByRef psfi As SHFILEINFO, _
    15.              ByVal cbFileInfo As Integer, _
    16.              ByVal uFlags As Integer) As IntPtr
    17.  
    18.     Private Const SHGFI_ICON = &H100
    19.     Private Const SHGFI_SMALLICON = &H1
    20.     Private Const SHGFI_LARGEICON = &H0    ' Large icon
    21.     Private Const MAX_PATH = 260
    22.     Private nIndex = 0
    23.  
    24.     Private Structure SHFILEINFO
    25.         Public hIcon As IntPtr            ' : icon
    26.         Public iIcon As Integer           ' : icondex
    27.         Public dwAttributes As Integer    ' : SFGAO_ flags
    28.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
    29.         Public szDisplayName As String
    30.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
    31.         Public szTypeName As String
    32.     End Structure
    33.  
    34.     Private Sub AddImages(ByVal strFileName As String)
    35.         Dim shInfo As SHFILEINFO
    36.         shInfo = New SHFILEINFO()
    37.         shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
    38.         shInfo.szTypeName = New String(vbNullChar, 80)
    39.         Dim hIcon As IntPtr
    40.         hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
    41.         Dim MyIcon As Drawing.Bitmap
    42.         MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
    43.         ImageList1.Images.Add(MyIcon)
    44.         nIndex = nIndex + 1
    45.     End Sub
    46.  
    47.     Sub getIcons(ByVal fnode As TreeNode, ByVal fname As String)
    48.         Dim ico As Icon = Icon.ExtractAssociatedIcon(fname)
    49.         If icoList.Contains(ico.Handle) Then '  if icolist already contains icon handle use index  
    50.             For i As Integer = 0 To icoList.Count
    51.                 If icoList(i) = ico.Handle Then
    52.                     fnode.ImageIndex = i
    53.                     fnode.SelectedImageIndex = i
    54.                 End If
    55.             Next
    56.         Else '  add to imagalist and icoList  
    57.             icoList.Add(ico.Handle)
    58.             ImageList1.Images.Add(ico)
    59.             fnode.ImageIndex = ImageList1.Images.Count - 1 '    ImageList1 starts at zero - so minus 1  
    60.             fnode.SelectedImageIndex = ImageList1.Images.Count - 1
    61.         End If
    62.     End Sub
    63.  
    64. #End Region
    65.  
    66. #Region "   PROJECT EXPLORER FILES / FOLDERS"
    67.  
    68.     Public Function getFolder(ByVal str As String) 'shorten path  
    69.         Dim i As Integer
    70.         Dim shrtPath As String
    71.         i = str.LastIndexOf("\")
    72.         shrtPath = str.Substring(i + 1)
    73.         Return shrtPath
    74.     End Function
    75.  
    76.     Public Sub load_files_treeview()
    77.         Try
    78.             TreeView1.Nodes.Clear()
    79.             Dim isRoot As String = (ToolStripTextBox1.Text) '   << Path of the folder
    80.             rootNode = TreeView1.Nodes.Add(isRoot)
    81.             rootNode.Tag = isRoot   '   name the node  
    82.  
    83.             For Each dir As String In My.Computer.FileSystem.GetDirectories(isRoot)
    84.                 addSubFolder(dir)
    85.             Next
    86.  
    87.             icoList.Add(1)
    88.             icoList.Add(2)
    89.  
    90.             loadFiles(rootNode, isRoot)
    91.  
    92.         Catch ex As Exception
    93.             MsgBox(ex.ToString)
    94.         End Try
    95.     End Sub
    96.  
    97.     Private Sub addSubFolder(ByVal dir As String)
    98.         Application.DoEvents()
    99.         Try
    100.             Dim shrtPath As String
    101.             shrtPath = getFolder(dir)
    102.             tNode = rootNode.Nodes.Add(shrtPath)
    103.             tNode.Tag = dir 'name the node
    104.  
    105.             tempNode = tNode.Nodes.Add("temp") 'add temp node
    106.             tempNode.Tag = ("temp") 'name the node
    107.         Catch ex As Exception
    108.  
    109.             MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    110.         End Try
    111.     End Sub
    112.  
    113.     Sub loadFolders(ByVal Tnode As TreeNode, ByVal fPath As String)
    114.         Application.DoEvents()
    115.         For Each folderNode As String In Directory.GetDirectories(fPath)
    116.             Dim shortNode As String
    117.             shortNode = getFolder(folderNode) 'shorten path  
    118.  
    119.             Dim subNode As TreeNode = Tnode.Nodes.Add(shortNode)
    120.             subNode.Tag = folderNode
    121.             subNode.Nodes.Add("temp")
    122.         Next
    123.         loadFiles(Tnode, fPath)
    124.     End Sub
    125.  
    126.     Sub loadFiles(ByVal fnode As TreeNode, ByVal dir As String)
    127.         Application.DoEvents()
    128.         For Each filesNode As String In Directory.GetFiles(dir)
    129.             Dim subFileNode As TreeNode = fnode.Nodes.Add(filesNode)
    130.             subFileNode.Tag = filesNode
    131.             subFileNode.Text = Path.GetFileName(filesNode)
    132.             getIcons(subFileNode, filesNode)
    133.         Next
    134.     End Sub
    135.  
    136.     Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    137.         Try
    138.             If TreeView1.SelectedNode.Nodes.Count = 1 & TreeView1.SelectedNode.Nodes(0).Text = "temp" Then
    139.                 TreeView1.SelectedNode.Nodes.Clear() 'if only temp node exists, delete it  
    140.             Else
    141.                 TreeView1.SelectedNode.Nodes.Clear() 'delete temp node  
    142.                 loadFolders(TreeView1.SelectedNode, TreeView1.SelectedNode.Tag.ToString)
    143.             End If
    144.             If TreeView1.SelectedNode.ToString.Contains(".") Then
    145.                 getIcons(TreeView1.SelectedNode, TreeView1.SelectedNode.Tag)
    146.             End If
    147.         Catch
    148.         End Try
    149.     End Sub
    150.  
    151.     Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
    152.         Try
    153.             If e.Node.Nodes.Count = 1 Then
    154.                 If e.Node.Nodes(0).Text.ToString = "temp" Then
    155.                     e.Node.Nodes.Clear()
    156.                     loadFolders(e.Node, e.Node.Tag.ToString)
    157.                 End If
    158.             End If
    159.         Catch
    160.         End Try
    161.     End Sub
    162.  
    163. #End Region
    164.  
    165.     '   ACTIONS "OK" > Load the files and (sub)folders
    166.  
    167.     Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
    168.         load_files_treeview()
    169.     End Sub
    170.  
    171.     '   ACTION "EXPAND" > Expand the treeview
    172.  
    173.     Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
    174.         TreeView1.ExpandAll()
    175.     End Sub
    176.  
    177.     '   ACTION "SORT" > Sort the files and folders asc.
    178.  
    179.     Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
    180.         TreeView1.Sort()
    181.     End Sub
    182.  
    183.     '   doubleclick a node and set the tag into the textbox
    184.  
    185.     Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
    186.         TextBox1.AppendText(e.Node.Tag & vbNewLine)
    187.     End Sub
    188. End Class

    If somone know how to speed things up, whatever, please let me know.
    Have fun with it and "happy coding and God bless"
    Last edited by Radjesh Klauke; May 15th, 2012 at 03:05 PM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width