How to expand a treeview node by code?

spebola

Regular
Joined
Mar 9, 2003
Messages
50
Location
Oklahoma City
I have a form with a treeview and a listview side by side. When the user selects a row in the listview, I want to expand the corresponding node in the treeview. I have tried the following code but it returns an exception (out of range):

Dim tvwIndex as Integer = lvw.FocusedItem.Index
tvw.Nodes(tvwIndex).Expand()

This code is in the Click event for the listview.

Any help would be appreciated.
 
You can't assume the index will be the same. Indexes start at 0 for each group of nodes under a parent. One way of doing this would be to assign a corresponding treenode to the Tag property of a ListViewItem, then casting it when required:

Visual Basic:
DirectCast(lvw.FocusedItem.Tag, TreeNode).Expand()
 
Back
Top