Resolved Select TreeView Node by ListView Selected Item

gyclone

Member
Joined
Jun 1, 2009
Messages
9
Programming Experience
1-3
Greetings,

I have been playing with, and researching, this all day and can't figure it out. I have a Windows Explorer style form with a TreeView and a ListView. I've populated the TreeView with directories and when a node is selected, the ListView fills with the contents of the directory associated with that node. No problem there, but I want to be able to select a directory in the ListView and automatically select the associated node in the TreeView, just like it works in Windows Explorer. All the information I've found online tells me how to populate the ListView from the TreeView, but I haven't found any good examples of how to populate the TreeView from the ListView.

I've been playing with this idea:


VB.NET:
Private Sub lstDirectoryInfo_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lstDirectoryInfo.ItemSelectionChanged

        ' lstDirectoryInfo is the ListView
        ' treeFolders is the TreeView

        For Each File As ListViewItem In lstDirectoryInfo.Items
            If File.Selected = True Then
                If File.SubItems(1).Text = "Directory" Then
                    'select that directory in tree view
                    treeFolders.SelectedNode = treeFolders.Nodes.Find(File.Text, True)(0)
                End If
            End If
        Next
    End Sub


But when I select the directory in the ListView I get an "Index is outside the bounds of the array" error.

Any suggestions would be greatly appreciated.

Thanks!
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,496
Location
Sydney, Australia
Programming Experience
10+
This will work whether the nodes have keys or not:
VB.NET:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, _
                                           ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged
    If Me.ListView1.SelectedItems.Count > 0 Then
        Dim itemText As String = Me.ListView1.SelectedItems(0).Text

        For Each node As TreeNode In Me.TreeView1.SelectedNode.Nodes
            If node.Text = itemText Then
                Me.TreeView1.SelectedNode = node
                Exit For
            End If
        Next
    End If
End Sub
This is more succinct but requires the nodes to have been added with keys that match the text:
VB.NET:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, _
                                           ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged
    If Me.ListView1.SelectedItems.Count > 0 Then
        Dim itemText As String = Me.ListView1.SelectedItems(0).Text
        Dim node As TreeNode = Me.TreeView1.SelectedNode.Nodes.Find(itemText, False).FirstOrDefault()

        If node IsNot Nothing Then
            Me.TreeView1.SelectedNode = node
        End If
    End If
End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,496
Location
Sydney, Australia
Programming Experience
10+
The only choices I see in the Thread Tools menu are:

Show Printable Version
Email This Page
Unsubscribe from this Thread

Ah, sorry. Wrong forum. :eek:

You need to edit your first post and change the title prefix to "Answered".
 
Top Bottom