Imports System.IO
Imports System.Collections.Generic
Public Class TreeViewListViewDemo
#Region " TreeView Code "
Private Function FixPath(ByVal path As String) As String
If path.EndsWith("\") Then
Return path
Else
Return path & "\"
End If
End Function
Private Sub FillDrives()
For Each drive As DriveInfo In DriveInfo.GetDrives()
Dim key As String = "Drive"
Dim name As String = drive.Name
' For drive names, remove the trailing "\":
If name.EndsWith("\") Then
name = name.Substring(0, name.Length - 1)
End If
key = GetImageKey(drive)
Dim node As TreeNode = _
FoldersTreeView.Nodes.Add(name, name, key, key)
' Add a node so you see a +.
node.Nodes.Add("FILLER")
Next
End Sub
Private Function GetImageKey( _
ByVal drive As DriveInfo) As String
Dim key As String
Select Case drive.DriveType
Case DriveType.CDRom
key = "CDDrive"
Case DriveType.Fixed
key = "Drive"
Case DriveType.Network
key = "NetworkDrive"
Case DriveType.Removable
key = "Floppy"
Case Else
key = "Drive"
End Select
Return key
End Function
Private Sub FilesTreeView_BeforeExpand( _
ByVal sender As Object, _
ByVal e As TreeViewCancelEventArgs) _
Handles FoldersTreeView.BeforeExpand
Try
Dim currentNode As TreeNode = e.Node
' Remove the filler node, if it's there:
currentNode.Nodes.Clear()
' Now go get all the folders.
Dim fullPathString As String = _
FixPath(currentNode.FullPath)
For Each folderString As String In _
Directory.GetDirectories(fullPathString)
' Handle each folder:
Dim fileName As String = Path.GetFileName(folderString)
Dim newNode As TreeNode = _
currentNode.Nodes.Add(fileName, fileName, _
"ClosedFolder", "OpenFolder")
' Add the filler node:
newNode.Nodes.Add("FILLER")
newNode.ToolTipText = newNode.FullPath
Next
Catch ex As Exception
MessageBox.Show(ex.Message, _
"Error Displaying Information")
End Try
End Sub
Private Sub FilesTreeView_AfterSelect( _
ByVal sender As System.Object, _
ByVal e As TreeViewEventArgs) _
Handles FoldersTreeView.AfterSelect
FillFiles(e.Node)
End Sub
#End Region
#Region " ListView Code "
' Store the current grouping column:
Private selectedColumn As Integer = 0
' Show groups?
Private showGroups As Boolean = False
' Keep track of the sort order:
Private order As SortOrder = SortOrder.None
Private Function AddIconToImageList(ByVal fileName As String) As String
' If the icon for the file isn't already stored
' within the image list, add it now.
Dim ext As String = Path.GetExtension(fileName)
If String.IsNullOrEmpty(ext) Then
ext = "Empty"
End If
If Not images.Images.ContainsKey(ext) Then
' Get the icon from the Windows association,
' and add it to the ImageList:
Dim ico As Icon = _
Drawing.Icon.ExtractAssociatedIcon(fileName)
images.Images.Add(ext, ico)
largeImages.Images.Add(ext, ico)
End If
Return ext
End Function
Private Sub FillFiles(ByVal node As TreeNode)
FileListView.Items.Clear()
Try
' Handle each file.
For Each fileString As String In _
Directory.GetFiles(FixPath(node.FullPath))
Dim fi As New FileInfo(fileString)
Dim ext As String = AddIconToImageList(fileString)
Dim lvi As New ListViewItem(fi.Name, ext)
lvi.SubItems.Add(fi.Length.ToString("N0"))
lvi.SubItems.Add( _
fi.LastWriteTime.ToShortDateString())
' Add the new item to the ListView control:
FileListView.Items.Add(lvi)
Next
' Note that you cannot show groups unless
' the operating system is Windows XP or later.
' See the Environment.OSVersion property for info
' on detecting the version:
If showGroups Then
' Although it's possible to group on columns
' other than the first, this demo doesn't.
SetGroups(0)
End If
Catch ex As Exception
' If the code fails, just fail silently.
End Try
End Sub
Private Sub FileListView_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileListView.DoubleClick
' If you double-click on a file, this code
' attempts to run the associated application:
Try
Process.Start(FixPath(FoldersTreeView.SelectedNode.FullPath) & _
FileListView.FocusedItem.Text)
Catch ex As Exception
MessageBox.Show("Unable to start: " & ex.Message, "Run Application")
End Try
End Sub
Private Sub SetGroups(ByVal column As Integer)
' Sets FileListView to the groups created for
' the filename column.
' Although you could ostensibly support grouping
' on any column, this example groups only on the
' file name column.
' Remove the current groups.
FileListView.Groups.Clear()
' Keep track of whether you've used the
' group name yet:
Dim groups As New List(Of String)
' Iterate through the items in FileListView.
For Each item As ListViewItem In FileListView.Items
' Retrieve the text value for the column.
Dim groupName As String = _
item.Text.Substring(0, 1).ToUpper()
' If the groups table does not already contain a group
' for the value, add a new group:
If groups.Contains(groupName) Then
' Group name exists: retrieve a reference:
item.Group = FileListView.Groups(groupName)
Else
' Group name doesn't exist. Add it now:
groups.Add(groupName)
item.Group = _
FileListView.Groups.Add(groupName, groupName)
End If
Next item
End Sub
Private Sub FileListView_ColumnClick( _
ByVal sender As System.Object, _
ByVal e As ColumnClickEventArgs) _
Handles FileListView.ColumnClick
Try
' Hide updates until you're done.
FileListView.BeginUpdate()
' Don't sort if showing groups:
If Not showGroups Then
' If you've clicked on the same column again,
' toggle the sort order. If it's a different column,
' sort ascending.
If e.Column <> selectedColumn Then
' Different column? Always sort ascending:
order = SortOrder.Ascending
Else
' Same column? Toggle the order:
If order = SortOrder.Descending Then
order = SortOrder.Ascending
Else
order = SortOrder.Descending
End If
End If
' Select the correct IComparer:
Dim sorter As IComparer = Nothing
Select Case e.Column
Case 0
' File name:
sorter = New _
ListViewStringSorter(e.Column, order)
Case 1
' File size:
sorter = New _
ListViewSizeSorter(e.Column, order)
Case 2
' File date:
sorter = New _
ListViewDateSorter(e.Column, order)
Case Else
' Any other column? Sort as a string:
sorter = New _
ListViewStringSorter(e.Column, order)
End Select
' Set the sorter, and sort the data.
' Setting the control's Sorting property
' also automatically sorts the data, but
' only on the first "column" of data:
FileListView.ListViewItemSorter = sorter
End If
Finally
FileListView.EndUpdate()
selectedColumn = e.Column
End Try
End Sub
Private Sub LargeIconToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LargeIconToolStripMenuItem.Click
FileListView.View = View.LargeIcon
End Sub
Private Sub DetailsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailsToolStripMenuItem.Click
FileListView.View = View.Details
End Sub
Private Sub SmallIconToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SmallIconToolStripMenuItem.Click
FileListView.View = View.SmallIcon
End Sub
Private Sub ListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListToolStripMenuItem.Click
FileListView.View = View.List
End Sub
Private Sub TileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileToolStripMenuItem.Click
FileListView.View = View.Tile
End Sub
Private Sub GroupFileNamesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupFileNamesToolStripMenuItem.Click
showGroups = Not showGroups
FileListView.ShowGroups = showGroups
GroupFileNamesToolStripMenuItem.Checked = Not GroupFileNamesToolStripMenuItem.Checked
FillFiles(FoldersTreeView.SelectedNode)
End Sub
Private Sub TreeViewContextMenuStrip_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TreeViewContextMenuStrip.Opening
LargeIconToolStripMenuItem.Checked = False
DetailsToolStripMenuItem.Checked = False
SmallIconToolStripMenuItem.Checked = False
ListToolStripMenuItem.Checked = False
TileToolStripMenuItem.Checked = False
GroupFileNamesToolStripMenuItem.Checked = False
Select Case FileListView.View
Case View.Details
DetailsToolStripMenuItem.Checked = True
Case View.LargeIcon
LargeIconToolStripMenuItem.Checked = True
Case View.List
ListToolStripMenuItem.Checked = True
Case View.SmallIcon
SmallIconToolStripMenuItem.Checked = True
Case View.Tile
TileToolStripMenuItem.Checked = True
End Select
GroupFileNamesToolStripMenuItem.Checked = FileListView.ShowGroups
End Sub
#End Region
Private Sub TreeViewListViewDemo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FileListView.SmallImageList = images
FileListView.LargeImageList = largeImages
FillDrives()
End Sub
End Class