Programmatically Clicking a Treenode

I have an explorer-like application, with a basic Treeview and Listview. When I click on a node in the treeview, it loads all items into the listview, including folders. I'd like to be able to programmatically click a node in the treeview, based upon if a folder was double-clicked in the listview.

Here's what I have so far:


'ListView DoubleClick
Private Sub lstItems_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstItems.DoubleClick
Dim SelectedItem As ListViewItem = New ListViewItem
SelectedItem = lstItems.SelectedItems(0)

If SelectedItem.SubItems(2).Text <> "File Folder" Then
Download(CurrentDirectory, SelectedItem.Text)
Else

End If
End Sub

'When user clicks on node, call Fill function, populating the treeview
Private Sub tvDir_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvDir.NodeMouseClick
tvDir.BeginUpdate()
Fill(e.Node)
tvDir.EndUpdate()
End Sub
Private Sub Fill(ByVal Node As TreeNode)
Node.Nodes.Clear()
lstItems.Items.Clear()

'...
'Code to Add items to listview and treeview omitted
'...

'Change the global variable
CurrentDirectory = Node.FullPath

End Sub

The problem is, when I try to declare a new node and either pass it through the nodeClick event, or call the Fill() subroutine directly, I get an error when I try to access the Node.FullPath property - "Full path can only be retrieved when a TreeNode has been added to a TreeView. This TreeNode has not been added to a TreeView."

How can I easily click the node needed when all I have is the text, ie: CurrentDirectory & "/" & SelectedItem.Text

Thanks for any suggestions.

-Jason

[2226 byte] By [OpticTygre] at [2007-12-16]
# 1
you will get better results with the following code if you use a unique key(1st arg in find method is node key) for each node on your tree...hopefully its a start at least :)



Dim MyFoundNodes() As TreeNode = Me.TreeView1.Nodes.Find(SelectedItem.Text, True)

Me.TreeView1.SelectedNode = MyFoundNodes(0)

MyFoundNodes(0).EnsureVisible()


DMan1 at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Exactly what I was looking for! Thanks!
OpticTygre at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...