ListView comuncate with Treeview

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
6 posts Page 1 of 1
Contributors
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

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
User avatar
noypikami
VIP - Donator
VIP - Donator
Posts: 151
Joined: Sat Dec 22, 2012 1:49 am

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================
Code: Select all
  Imports System.IO  
===== Image 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 all
Private 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.
Last edited by noypikami on Wed Feb 06, 2013 5:08 am, edited 2 times in total.
User avatar
noypikami
VIP - Donator
VIP - Donator
Posts: 151
Joined: Sat Dec 22, 2012 1:49 am

i forgot one thing.... this code populate the treeview as well as the list view and vice versa.
you van expand and collapse the treeview or the list view.....
if you click an item in listview it will show up the file(process.start) .....


i hope this helps!
==============cheers=================================



REFERENCE: some codes i used came from here...
http://social.msdn.microsoft.com/Forums ... 589199b6db
User avatar
muttley1968
Hardcore Programmer
Hardcore Programmer
Posts: 622
Joined: Thu Jun 17, 2010 11:54 pm

Umm i cant right now show you my code but ill sort it out soon and post my code so you know what i mean :P that wont work for me because i dont have an open file dialog doing my treeview :(
User avatar
Scottie1972
Codenstuff Elite
Codenstuff Elite
Posts: 953
Joined: Thu Jan 14, 2010 5:48 am

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

Here: http://forums.codeguru.com/showthread.p ... pplication


NOTE: if your uses Win7 or Win8 this tutorial will only be a good guideline to follow.

this tutorial will show you everything you need to do in order to make a nice "Windows Explorer" clone application.
It will even show you how to get associated icons if you want them.
Image
User avatar
noypikami
VIP - Donator
VIP - Donator
Posts: 151
Joined: Sat Dec 22, 2012 1:49 am

muttley1968 wrote:
Umm i cant right now show you my code but ill sort it out soon and post my code so you know what i mean :P that wont work for me because i dont have an open file dialog doing my treeview :(
oh i see....! you can relate the code to something else if not on open file dialog to open files.
for example you have a database, you can relate the treeview with it when the form loads.
instead of opening or adding files manually to treeview.
on your question you said "comunicate list view with tree view and vice versa...."

just make a few adjustments on the codes.
6 posts Page 1 of 1
Return to “Coding Help & Support”