Advertisement
JayBeeOH

TreeView and ListView Demo

May 13th, 2015
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.52 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2015. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   TreeView and ListView Demo
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   May 2015
  16. '
  17. ' Description:    Demo of loading the TreeView and ListView items.
  18. '                 This project mocks the File Explorer.
  19. '
  20. '                 Documentation is at:
  21. '                   App's screen image is at: http://imgur.com/uvUiYX5
  22. '                   App's Visual Basic .NET code is at http://pastebin.com/7UsC6byf
  23. '                   Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
  24. '-------------------------------------------------------------------------------------------
  25.  
  26. Imports System.IO
  27. Imports System.Collections.ObjectModel
  28.  
  29. Public Class MainForm
  30.  
  31.     Private rootNode As TreeNode
  32.     Private isFirstPaint As Boolean = True
  33.  
  34.  
  35. #Region " Form related events"
  36.  
  37.     Private Sub MainForm_Load(sender As Object, e As EventArgs) _
  38.         Handles Me.Load
  39.  
  40.         Dim startingFolder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
  41.         rootNode = TreeView1.Nodes.Add(startingFolder)
  42.  
  43.         With ListView1
  44.             .SmallImageList = ImageList1
  45.             .View = View.Details
  46.  
  47.             ' Add Header columns to ListView
  48.             Dim header1, header2, header3, header4 As New ColumnHeader
  49.  
  50.             With header1
  51.                 .Text = "File Name"
  52.                 .TextAlign = HorizontalAlignment.Left
  53.                 .Width = 200
  54.             End With
  55.  
  56.             With header2
  57.                 .Text = "File Size (bytes)"
  58.                 .TextAlign = HorizontalAlignment.Right
  59.                 .Width = 100
  60.             End With
  61.  
  62.             With header3
  63.                 .Text = "Created"
  64.                 .TextAlign = HorizontalAlignment.Right
  65.                 .Width = 90
  66.             End With
  67.  
  68.             With header4
  69.                 .Text = "Last Accessed"
  70.                 .TextAlign = HorizontalAlignment.Right
  71.                 .Width = 90
  72.             End With
  73.  
  74.             .Columns.Add(header1)
  75.             .Columns.Add(header2)
  76.             .Columns.Add(header3)
  77.             .Columns.Add(header4)
  78.         End With
  79.     End Sub
  80.  
  81.     ' Painting of the form.
  82.     Private Sub MainForm_Paint(sender As Object, e As PaintEventArgs) _
  83.         Handles Me.Paint
  84.  
  85.         'Since the LoadTreeView procedure may take some time,
  86.         'it is done after the form has loaded.
  87.         If isFirstPaint Then
  88.             Me.Cursor = Cursors.WaitCursor
  89.             LoadTreeView(rootNode)
  90.             Me.Cursor = Cursors.Default
  91.             isFirstPaint = False
  92.         End If
  93.     End Sub
  94.  
  95. #End Region
  96.  
  97. #Region " TreeView related events"
  98.  
  99.     ' Add subfolders to TreeView.
  100.     Private Sub TreeView1_AfterExpand(sender As Object, e As TreeViewEventArgs) _
  101.         Handles TreeView1.AfterExpand
  102.  
  103.         LoadTreeView(e.Node)
  104.     End Sub
  105.  
  106.     ' When TreeView node is selected, load ListView.
  107.     Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) _
  108.         Handles TreeView1.AfterSelect
  109.  
  110.         LoadListView(e.Node.FullPath)
  111.     End Sub
  112.  
  113.     ' Load the TreeView.
  114.     Private Sub LoadTreeView(ByRef currentNode As TreeNode)
  115.  
  116.         Dim rootDirectory = currentNode.FullPath
  117.         Dim allFolders As ReadOnlyCollection(Of String)
  118.  
  119.         Try
  120.             allFolders = My.Computer.FileSystem.GetDirectories(rootDirectory)
  121.         Catch ex As UnauthorizedAccessException
  122.             currentNode.Text &= " *** Not Authorized ***"
  123.             Exit Sub
  124.         End Try
  125.  
  126.         currentNode.Nodes.Clear()
  127.  
  128.         For Each folder In allFolders
  129.             Dim aTreeNode As New TreeNode(My.Computer.FileSystem.GetDirectoryInfo(folder).Name)
  130.  
  131.             Try
  132.                 For Each subFolder In My.Computer.FileSystem.GetDirectories(folder)
  133.                     Dim childNode As New TreeNode(My.Computer.FileSystem.GetDirectoryInfo(subFolder).Name)
  134.                     aTreeNode.Nodes.Add(childNode)
  135.                 Next
  136.             Catch ex As UnauthorizedAccessException
  137.                 aTreeNode.Nodes.Add("*** Not Authorized ***")
  138.             Catch ex As Exception
  139.                 ' Other types of errors.
  140.             End Try
  141.  
  142.             currentNode.Nodes.Add(aTreeNode)
  143.             TreeView1.Refresh()
  144.         Next
  145.     End Sub
  146.  
  147. #End Region
  148.  
  149. #Region " ListView related events"
  150.  
  151.     ' Load the ListView.
  152.     Private Sub LoadListView(ByVal fullPath As String)
  153.  
  154.         ListView1.Items.Clear()
  155.  
  156.         Dim files As ReadOnlyCollection(Of String)
  157.  
  158.         Try
  159.             files = My.Computer.FileSystem.GetFiles(fullPath)
  160.  
  161.             For Each file In files
  162.  
  163.                 Dim fInfo As FileInfo = My.Computer.FileSystem.GetFileInfo(file)
  164.  
  165.                 ' Check ImageList to see if an icon is already associated
  166.                 ' for this file type, if not, try to add one.
  167.                 If Not (ImageList1.Images.ContainsKey(fInfo.Extension)) Then
  168.                     Dim aIcon As Icon = SystemIcons.WinLogo
  169.                     Try
  170.                         aIcon = System.Drawing.Icon.ExtractAssociatedIcon(fInfo.FullName)
  171.                     Catch ex As Exception
  172.                         ' error getting icon.
  173.                     End Try
  174.                     ImageList1.Images.Add(fInfo.Extension, aIcon)
  175.                 End If
  176.  
  177.                 Dim lvItem As New ListViewItem(fInfo.Name, fInfo.Extension)
  178.                 With lvItem.SubItems
  179.                     .Add(fInfo.Length.ToString("N0"))
  180.                     .Add(fInfo.CreationTime.ToShortDateString)
  181.                     .Add(fInfo.LastAccessTime.ToShortDateString)
  182.                 End With
  183.                 ListView1.Items.Add(lvItem)
  184.             Next
  185.         Catch ex As Exception
  186.             ' Most likely not authorized.
  187.         End Try
  188.  
  189.         ListView1.Refresh()
  190.     End Sub
  191.  
  192. #End Region
  193.  
  194. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement