muttley1968 wrote:Hi i was wondering if it is somehow possible to make a Listview and Treeview share the same thing i am trying to make a Folder broweser i have the Treeview working is their anyway to make it so the listview shows the same data as the Treeview so if someone clicks on say my documents in the listview the Treeview will expand the same feild
Hi ..! i found the solution to your problem (communicate treview to list view and vise versa). it takes time to find the solution but here it is with added features.....
=====first import this================
=====

then add this below public class form1
Code: Select all Dim permanent As String
Dim r As Boolean
=====add this code to your openfiledialog or folderbrowserdialog=======
Code: Select all Dim fb As New FolderBrowserDialog
If fb.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim path = fb.SelectedPath
Dim rootNode As TreeNode
Dim info As New DirectoryInfo(path)
If info.Exists Then
rootNode = New TreeNode(info.FullName)
rootNode.Tag = info
GetDirectories(info.GetDirectories(), rootNode)
TV.Nodes.Add(rootNode)
End If
LV.Items.Add(path)
permanent = path
End If
=== add this sub before "end class"================
Code: Select all Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, ByVal nodeToAddTo As TreeNode)
Dim aNode As TreeNode
Dim subSubDirs() As DirectoryInfo
Dim subDir As DirectoryInfo
For Each subDir In subDirs
aNode = New TreeNode(subDir.Name, 0, 0)
aNode.Tag = subDir
aNode.ImageKey = "folder"
subSubDirs = subDir.GetDirectories()
If subSubDirs.Length <> 0 Then
GetDirectories(subSubDirs, aNode)
End If
nodeToAddTo.Nodes.Add(aNode)
Next subDir
End Sub
=============add this to your treeview.NodeMouseClick=================
Try
Dim newSelected As TreeNode = e.Node
LV.Items.Clear()
Dim nodeDirInfo As DirectoryInfo = _
CType(newSelected.Tag, DirectoryInfo)
Dim subItems() As ListViewItem.ListViewSubItem
Dim item As ListViewItem = Nothing
LV.Items.Add(permanent)
Dim dir As DirectoryInfo
For Each dir In nodeDirInfo.GetDirectories()
item = New ListViewItem(dir.FullName, 0)
subItems = New ListViewItem.ListViewSubItem() _
{New ListViewItem.ListViewSubItem(item, "Directory"), _
New ListViewItem.ListViewSubItem(item, _
dir.LastAccessTime.ToShortDateString())}
item.SubItems.AddRange(subItems)
LV.Items.Add(item)
Next dir
Dim file As FileInfo
For Each file In nodeDirInfo.GetFiles()
item = New ListViewItem(file.FullName, 1)
subItems = New ListViewItem.ListViewSubItem() _
{New ListViewItem.ListViewSubItem(item, "File"), _
New ListViewItem.ListViewSubItem(item, _
file.LastAccessTime.ToShortDateString())}
item.SubItems.AddRange(subItems)
LV.Items.Add(item)
Next file
LV.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
=====then add this to List view mouse doubleclick=================
Code: Select allPrivate Sub LV_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles LV.MouseDoubleClick
Try
Dim filepath As String = permanent
For Each item As ListViewItem In LV.Items
Dim f As String = item.SubItems(0).Text
If item.Selected = True And Not f = filepath Then
Process.Start(f)
End If
If f = filepath And r = Nothing Then
TV.CollapseAll()
LV.Items.Clear()
LV.Items.Add(permanent)
r = True
ElseIf f = filepath And r = True Then
TV.ExpandAll()
Dim path = filepath
Dim di As New IO.DirectoryInfo(path)
Dim dir As IO.DirectoryInfo()
dir = di.GetDirectories()
For Each directory In dir
LV.Items.Add(directory.FullName)
Next
r = False
End If
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
i insert here the dowload link for the whole project with full codes made in vb 2010


This file is hosted off-site.