Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2015. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: TreeView and ListView Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: May 2015
- '
- ' Description: Demo of loading the TreeView and ListView items.
- ' This project mocks the File Explorer.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/uvUiYX5
- ' App's Visual Basic .NET code is at http://pastebin.com/7UsC6byf
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Imports System.IO
- Imports System.Collections.ObjectModel
- Public Class MainForm
- Private rootNode As TreeNode
- Private isFirstPaint As Boolean = True
- #Region " Form related events"
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles Me.Load
- Dim startingFolder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
- rootNode = TreeView1.Nodes.Add(startingFolder)
- With ListView1
- .SmallImageList = ImageList1
- .View = View.Details
- ' Add Header columns to ListView
- Dim header1, header2, header3, header4 As New ColumnHeader
- With header1
- .Text = "File Name"
- .TextAlign = HorizontalAlignment.Left
- .Width = 200
- End With
- With header2
- .Text = "File Size (bytes)"
- .TextAlign = HorizontalAlignment.Right
- .Width = 100
- End With
- With header3
- .Text = "Created"
- .TextAlign = HorizontalAlignment.Right
- .Width = 90
- End With
- With header4
- .Text = "Last Accessed"
- .TextAlign = HorizontalAlignment.Right
- .Width = 90
- End With
- .Columns.Add(header1)
- .Columns.Add(header2)
- .Columns.Add(header3)
- .Columns.Add(header4)
- End With
- End Sub
- ' Painting of the form.
- Private Sub MainForm_Paint(sender As Object, e As PaintEventArgs) _
- Handles Me.Paint
- 'Since the LoadTreeView procedure may take some time,
- 'it is done after the form has loaded.
- If isFirstPaint Then
- Me.Cursor = Cursors.WaitCursor
- LoadTreeView(rootNode)
- Me.Cursor = Cursors.Default
- isFirstPaint = False
- End If
- End Sub
- #End Region
- #Region " TreeView related events"
- ' Add subfolders to TreeView.
- Private Sub TreeView1_AfterExpand(sender As Object, e As TreeViewEventArgs) _
- Handles TreeView1.AfterExpand
- LoadTreeView(e.Node)
- End Sub
- ' When TreeView node is selected, load ListView.
- Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) _
- Handles TreeView1.AfterSelect
- LoadListView(e.Node.FullPath)
- End Sub
- ' Load the TreeView.
- Private Sub LoadTreeView(ByRef currentNode As TreeNode)
- Dim rootDirectory = currentNode.FullPath
- Dim allFolders As ReadOnlyCollection(Of String)
- Try
- allFolders = My.Computer.FileSystem.GetDirectories(rootDirectory)
- Catch ex As UnauthorizedAccessException
- currentNode.Text &= " *** Not Authorized ***"
- Exit Sub
- End Try
- currentNode.Nodes.Clear()
- For Each folder In allFolders
- Dim aTreeNode As New TreeNode(My.Computer.FileSystem.GetDirectoryInfo(folder).Name)
- Try
- For Each subFolder In My.Computer.FileSystem.GetDirectories(folder)
- Dim childNode As New TreeNode(My.Computer.FileSystem.GetDirectoryInfo(subFolder).Name)
- aTreeNode.Nodes.Add(childNode)
- Next
- Catch ex As UnauthorizedAccessException
- aTreeNode.Nodes.Add("*** Not Authorized ***")
- Catch ex As Exception
- ' Other types of errors.
- End Try
- currentNode.Nodes.Add(aTreeNode)
- TreeView1.Refresh()
- Next
- End Sub
- #End Region
- #Region " ListView related events"
- ' Load the ListView.
- Private Sub LoadListView(ByVal fullPath As String)
- ListView1.Items.Clear()
- Dim files As ReadOnlyCollection(Of String)
- Try
- files = My.Computer.FileSystem.GetFiles(fullPath)
- For Each file In files
- Dim fInfo As FileInfo = My.Computer.FileSystem.GetFileInfo(file)
- ' Check ImageList to see if an icon is already associated
- ' for this file type, if not, try to add one.
- If Not (ImageList1.Images.ContainsKey(fInfo.Extension)) Then
- Dim aIcon As Icon = SystemIcons.WinLogo
- Try
- aIcon = System.Drawing.Icon.ExtractAssociatedIcon(fInfo.FullName)
- Catch ex As Exception
- ' error getting icon.
- End Try
- ImageList1.Images.Add(fInfo.Extension, aIcon)
- End If
- Dim lvItem As New ListViewItem(fInfo.Name, fInfo.Extension)
- With lvItem.SubItems
- .Add(fInfo.Length.ToString("N0"))
- .Add(fInfo.CreationTime.ToShortDateString)
- .Add(fInfo.LastAccessTime.ToShortDateString)
- End With
- ListView1.Items.Add(lvItem)
- Next
- Catch ex As Exception
- ' Most likely not authorized.
- End Try
- ListView1.Refresh()
- End Sub
- #End Region
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement