Get treenode value from vb.net 2005 winforms treeview

  • Thread starter Thread starter Karl Rhodes
  • Start date Start date
K

Karl Rhodes

Hi all...

Perhaps I'm not going about this the right way, but I wondered if
anyone can help?

I have a treeview which populates from a datatable using a parent/
child relationship. It stores a company/branch hierarchical structure
and show areas/regions/branches etc by name in it's nodes.

What I need to be able to do is select a node (by clicking on the
branch name etc) and return an integer value for the given hierarchy
point so that I can save data based on this integer value. The reason
I need this integer is because branches etc within the company
hierarchy may be the same, but reside in different trees of the
treeview.

I've noticed there's no Node.Value or anything and wondered if anyone
had had the same problem or if anyone could think of a way around this
problem?

Thanks
 
I'd also like to know how to dynamically set a SelectedNode and keep
it highlighted after the control has lost focus...
 
There's nothing to stop you creating your own TreeNode [class] and
adding whatever data fields you want to it, as in

Class CustomNode
Inherits TreeNode

Public Sub New()
MyBase.New()
End Sub

Public Sub New( value as ... )
Me.Value = value
End Sub

Public Property Value() As ...
. . .
End Property

End Class

.... then ...

Dim oTreeNode as New CustomNode( SomeValue )
tree1.Nodes.add(oTreeNode)

Then, when you click on any TreeNode, test its type and, if it's one of
yours, cast it and work with it:

Sub Tree1_AfterSelect( ... ) Handles tree1.AfterSelect

If TypeOf e.Node Is CustomNode Then
With DirectCast( e.Node, CustomNode )
DoSomethingWith( .Value )
End With
Else
End If

End Sub

Setting the SelectedNode is as easy as .. er .. setting the
SelectedNode, as in

tree1.SelectedNode = oTreeNode

oTreeNode.EnsureVisible ' make sure we can see the new selection
tree1.HideSelection = False ' make sure it /stays/ highlighted

HTH,
Phill W.
 
There's nothing to stop you creating your own TreeNode [class] and
adding whatever data fields you want to it, as in

Class CustomNode
Inherits TreeNode

Public Sub New()
MyBase.New()
End Sub

Public Sub New( value as ... )
Me.Value = value
End Sub

Public Property Value() As ...
. . .
End Property

End Class

... then ...

Dim oTreeNode as New CustomNode( SomeValue )
tree1.Nodes.add(oTreeNode)

Then, when you click on any TreeNode, test its type and, if it's one of
yours, cast it and work with it:

Sub Tree1_AfterSelect( ... ) Handles tree1.AfterSelect

If TypeOf e.Node Is CustomNode Then
With DirectCast( e.Node, CustomNode )
DoSomethingWith( .Value )
End With
Else
End If

End Sub

Thanks. I'll have a go at that!


Setting the SelectedNode is as easy as .. er .. setting the
SelectedNode, as in

tree1.SelectedNode = oTreeNode

Oh yeah, silly me! :)
 
There's nothing to stop you creating your own TreeNode [class] and
adding whatever data fields you want to it, as in
Class CustomNode
Inherits TreeNode
Public Sub New()
MyBase.New()
End Sub
Public Sub New( value as ... )
Me.Value = value
End Sub
Public Property Value() As ...
. . .
End Property
End Class
... then ...
Dim oTreeNode as New CustomNode( SomeValue )
tree1.Nodes.add(oTreeNode)
Then, when you click on any TreeNode, test its type and, if it's one of
yours, cast it and work with it:
Sub Tree1_AfterSelect( ... ) Handles tree1.AfterSelect
If TypeOf e.Node Is CustomNode Then
With DirectCast( e.Node, CustomNode )
DoSomethingWith( .Value )
End With
Else
End If

Thanks. I'll have a go at that!


Setting the SelectedNode is as easy as .. er .. setting the
SelectedNode, as in
tree1.SelectedNode = oTreeNode

Oh yeah, silly me! :)


oTreeNode.EnsureVisible ' make sure we can see the new selection
tree1.HideSelection = False ' make sure it /stays/ highlighted
HTH,
Phill W.


You can use the .Tag property of the Treenode Object to store the data
that you want to associate with the node. It accepts any object.

See the msdn documentation linked below for more information
http://msdn2.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx
 
Back
Top