473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Get treenode value from vb.net 2005 winforms treeview

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

Mar 6 '07 #1
4 8089
I'd also like to know how to dynamically set a SelectedNode and keep
it highlighted after the control has lost focus...

On 6 Mar, 12:34, "Karl Rhodes" <googlegro...@tlbsolutions.comwrote:
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

Mar 6 '07 #2

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.

Karl Rhodes wrote:
I'd also like to know how to dynamically set a SelectedNode and keep
it highlighted after the control has lost focus...
On 6 Mar, 12:34, "Karl Rhodes" <googlegro...@tlbsolutions.comwrote:
>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.
.. . .
>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?
Mar 6 '07 #3
On 6 Mar, 15:18, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
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! :)

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

HTH,
Phill W.


Mar 6 '07 #4
On Mar 6, 10:54 am, "Karl Rhodes" <googlegro...@tlbsolutions.com>
wrote:
On 6 Mar, 15:18, "Phill W." <p-.-a-.-w-a-r...@-o-p-e-n-.-a-c-.-u-k>
wrote:
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! :)
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/lib...enode.tag.aspx

Mar 6 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dean L. Howen | last post by:
Hi, I want to add some attributes to TreeNode, so I create a new class MyNode that inheritance from System.Windows.Forms.TreeNode, I want to TreeView use MyNode instead of TreeNode, so I can...
5
by: nevin | last post by:
Hi, If I have a TreeView with loads of Nodes and they all have children etc, how do I find the index of a given node when I only know the Text value? If I use TreeView.Nodes.IndexOf(new...
9
by: Mark Allison | last post by:
Hi there, I'm still very much a newbie to C#, and would like to derive from TreeNode to extend the TreeNode class. Here's test program: http://www.vkarlsen.no/pastebin/default.asp?id=4090 I...
6
by: Craig Lister | last post by:
I have a treeview with a Root, a Contact Type, and a Contact. Like MSN Messenger. Is there a way that I can say something like if treenode.tag is classContact then string Surname =...
3
by: markaelkins | last post by:
Hi. I am trying to enter a variable in the treenodesrc of a treenode. I am basically trying to send an ID variable into sql to return different records. I've searched everywhere and cannot find the...
0
by: Corky Whiteboard | last post by:
I have a Winforms treeview with subclassed treenodes. The treeview has "ShowNodeToolTips" set to true. The text is showing for each node, but it is not formatted 'pretty' - it comes out in one...
3
by: tanya foster | last post by:
Hello, I am re-writing a visual basic .net application(visual studio 2003) in an asp.net application(visual studio 2005). The vb.net application relied on a treeview and hence, treenodes. The...
3
by: Jeff | last post by:
Hey ..NET 2.0 I'm trying to search a TreeView control for a specific TreeNode The code below doesn't work because it only searches the the top level of the nodes. I would like to program it...
0
by: vincent90152900 | last post by:
How to pop up different text base on the selected TreeNode? I want to pop up different Text base on the selected TreeNode of the TreeView component. So, I create a TreeView and a ModalPopupExtender....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.