Notifications
Clear all

[Closed] Selecting item in dotnet treeview

 PEN

I have a dotNet tree view control and I want to have one of the items selected when it opens. How is this done? I have been poking around for ages. I can get what is selected just not set what is selected. Any ideas?

10 Replies

To set a dotNet TreeView selected node, just assign to the SelectedNode property the node (TreeNode value) you want to be selected.

This is probably extraoridinarly obvious, but make sure that the tree is populated first

 PEN

Ah, so I gather there is no way to do it directly with the index. I will have to get the node that I want and then pass that into the selectedNode property.

I will give this a try. Thanks. Oh, and the tree is populated.

 PEN

Here it is,

tv.selectedNode=tv.nodes.item[0]

This will select the first node in the tree.

Thanks guys.

 PEN

Looking back at my test code I did try this, I on way to much allergy meds to remember anything this time of year. how ever I didn’t think that it was working because the node that is selected will not show if the treeview is not in focus. This is unlike list boxes in Max where you can still see what is in focus. To get around this problem I’m having to set the back ground colour instead which will show up. This is working very well. Just a heads up for others.

You can also force focus calling tv.Focus().

 PEN

Didn’t work in my case as I don’t want focus on the tv control. I just want to know visualy what is selected in the list.

This forces the selected item to be visible even when the treeview is not in focus:
tv.HideSelection = false

Martijn

Thanks for the info Martjin.
Never seen this property before. It works also with ListView controls.

Page 1 / 2