Setting the start node on a treeview

Sean Kelly

Member
Joined
Dec 28, 2005
Messages
12
Programming Experience
10+
HI,

I have a treeview which i expand on loading the form, however as it no longer fits on the screen, the first node selected is now half way down the tree. Is there anyway to force the treeview to a certain node, either the top node or even anywhere specific?

Thanks for any help
:confused:
 
Treeview.SelectedNode
"When you set this property, the specified node is scrolled into view and any parent nodes are expanded so that the specified node is visible."
 
I'm trying without success to get this to work, I have the following code in, but still no luck what silly mistake am I making now?

Dim testnode As New TreeNode("TagDB")
Me.TV_Main.SelectedNode = testnode

 
Perhaps you have to add the TreeNode to the TreeView before the Treeview can select it?
 
Well this is the part I don't really understand as the node already exists in the tree, I just want to select that item, but can't seem to get it to work, everytime it loads the form, it runs through the treeview_afterselect subroutine and the node which is positioned at the top of the form rather than the top of the tree is classed as the selected node. This causes me a problem as it then goes and loads in the wrong information on to my start up form.
 
If the node already exists in tree then I don't understand the code in your question above. What you do in the two codes lines above is to create a new treenode and immediately try to set it as SelectedTab for treeview, but you didn't actually add this node to tree.

Is the problem that you don't know how to add a treenode to treeview, or don't you know how to find an existing treenode?
 
Sorry John.

The part i don't seem to be able to grasp is the moving the selected node from SOW node which is highlighted on screen load to teh TAGDB node that I would like to be classed as the selected node on screen load.

Thanks again for your time
 
If you have problems finding a node by key it could be that you haven't set a key yet. The key of a TreeNode is its Name, not its Text. You can set the Name property when you create the node, or when adding it to a TreeNodeCollection. Of course the key and name can be the same text.

If a node has a key it can be access directly from the default item property like this TreeView.Nodes(22).Nodes("key") or from one of the search facility methods of TreeNodeCollection like TreeView.Nodes.Find or TreeView.Nodes.IndexOfKey.

Examples using different Add method overloads of TreeNodeCollection, and working with TreeNode properties:
VB.NET:
TreeView.Nodes.Add("key", "text")
VB.NET:
Dim tn As New TreeNode("TAGDB")
tn.Name = "TAGDB"
TreeView1.Nodes.Add(tn)

If you don't want to do this you can still access the nodes by index. Also you can search all the nodes to see if there is a node with a specific text. Example search nodes by Text property:
VB.NET:
'usage example:
'TreeView1.SelectedNode = getTreeNodeByText(TreeView1.Nodes, "TAGDB")
 
Function getTreeNodeByText(ByVal tnc As TreeNodeCollection, ByVal text As String) As TreeNode
  Dim tnfind As TreeNode
  For Each tn As TreeNode In tnc
    If tn.Text = text Then Return tn
    If tn.Nodes.Count > 0 Then
      tnfind = getTreeNodeByText(tn.Nodes, text)
      If Not tnfind Is Nothing Then Return tnfind
   End If
  Next
  Return Nothing
End Function
Another thing you can do is if always one particular node is wanted to work with later is to keep the reference to this node in a global variable when you create it, and later just use this reference.
Example:
VB.NET:
Dim tn As TreeNode
 
Sub creator()
  tn = New TreeNode("sumt")
  TreeView1.Nodes.Add(tn)
End Sub
 
Sub accessor()
  TreeView1.SelectedNode = tn
End sub
 
Back
Top