PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Finding my position in a Treeview

Reply

Finding my position in a Treeview

 
Thread Tools Rate Thread
Old 05-09-2003, 06:50 PM   #1
meh
Guest
 
Posts: n/a
Default Finding my position in a Treeview


I can figure out the total number of nodes in a given tree but what I'd like
to know is what is the Selected Nodes relationship to the entire tree i.e
This is node n out of nnn nodes.
In most of my database apps its common place to tell the user that they are
working on record x of xxx records I'm trying to provide the same
functionality using the tree control.


TIA
meh


  Reply With Quote
Old 05-09-2003, 07:43 PM   #2
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
Default Re: Finding my position in a Treeview

"meh" <hogberg@qwest.net> schrieb:
> I can figure out the total number of nodes in a given
> tree but what I'd like to know is what is the Selected
> Nodes relationship to the entire tree i.e This is node
> n out of nnn nodes.
> In most of my database apps its common place to tell the
> user that they are working on record x of xxx records I'm
> trying to provide the same functionality using the tree control.


You can grab the node's 'Index' property value, then get its 'Parent' node,
add its index to the node number and so on, until you reach the root node.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


  Reply With Quote
Old 05-09-2003, 07:52 PM   #3
Vijay Balki
Guest
 
Posts: n/a
Default Re: Finding my position in a Treeview

Meh,

There is no inherent property or method available to support your request in
Treeview. You can know the current node's Parent node and what is the number
of the node relative to only its parent. Now if you are looking to know the
number relative all nodes, you have to programmatically keep track of it in
a variable..

VJ

"meh" <hogberg@qwest.net> wrote in message
news:esLRnY9cDHA.2804@TK2MSFTNGP11.phx.gbl...
> I can figure out the total number of nodes in a given tree but what I'd

like
> to know is what is the Selected Nodes relationship to the entire tree i.e
> This is node n out of nnn nodes.
> In most of my database apps its common place to tell the user that they

are
> working on record x of xxx records I'm trying to provide the same
> functionality using the tree control.
>
>
> TIA
> meh
>
>



  Reply With Quote
Old 05-09-2003, 08:52 PM   #4
Fergus Cooney
Guest
 
Posts: n/a
Default Re: Finding my position in a Treeview

Hi Meh, Herfried,

|| You can grab the node's 'Index' property value,
|| then get its 'Parent' node, add its index to the node
|| number and so on, until you reach the root node.

This back-up-the-hierarchy method is a good way to get screen positions of a control within a series of containers. It works
because each X, say, is a position relative to the left of the parent, and it doesn't matter whether there are any other controls
inbetween or anywhere else..

Unfortunately it doesn't work with indexes. :-( For example, if a node is the fourth under its parent, its index within the tree
will depend on the number of sub-nodes that each of the other three has.

The hard method is to go back up the hierarchy, and then recurse down each of the nodes that precede the node in question at any
given level.

If all this sound like gobbledegook, just ignore it :-) If it makes sense to you then so might the following code.

In short, I would recommend that you set the Tag of the Nodes in the tree. This assumes, though, that your Tree contents will
remain stable. Any additions or deletions would spoil the count.

Regards,
Fergus

Private Sub TheTreeView_AfterSelect(ByVal sender As System.Object, _
ByVal e AsSystem.Windows.Forms.TreeViewEventArgs) _
Handles TheTreeView.AfterSelect
Label1.Text = NodePosition (TheTreeView, e.Node)
End Sub

'Determine the position (1-based) of the given Node in its TreeView.
Private Function NodePosition (oTreeView As TreeView, oNode As TreeNode)
Dim iPosInTree As Integer = 0
Do
Dim iNodeIndex As Integer = oNode.Index
iPosInTree = iPosInTree + iNodeIndex + 1

'Get the Parent Node or the TreeView if at the top.
Dim oParentNode As Object = oNode.Parent
If oParentNode Is Nothing Then
oParentNode = oTreeView
End If

'Count the Nodes precding this one on the current level.
Dim I As Integer
For I = 0 To iNodeIndex - 1
iPosInTree = iPosInTree + NumberOfChildren (oParentNode.Nodes (I))
Next

'Go up to the next level.
oNode = oNode.Parent
Loop Until oNode Is Nothing
Return iPosInTree
End Function

Function NumberOfChildren (oNode As TreeNode)
If oNode.LastNode Is Nothing Then
Return 0 'No children
End If
Dim iNumChildren = oNode.LastNode.Index + 1
Dim oSubNode As TreeNode
For Each oSubNode in oNode.Nodes
iNumChildren = iNumChildren + NumberOfChildren (oSubNode)
Next
Return iNumChildren
End Function


  Reply With Quote
Old 05-09-2003, 09:37 PM   #5
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
Default Re: Finding my position in a Treeview

"Herfried K. Wagner [MVP]" <hirf.nosp@m.activevb.de> schrieb:
> You can grab the node's 'Index' property value, then get its 'Parent'

node,
> add its index to the node number and so on, until you reach the root node.


The last part of the sentence is nonsense. See Fergus' explanation. I
should have more sleep.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


  Reply With Quote
Old 06-09-2003, 02:59 AM   #6
meh
Guest
 
Posts: n/a
Default Re: Finding my position in a Treeview THX to all

THX
"meh" <hogberg@qwest.net> wrote in message
news:esLRnY9cDHA.2804@TK2MSFTNGP11.phx.gbl...
> I can figure out the total number of nodes in a given tree but what I'd

like
> to know is what is the Selected Nodes relationship to the entire tree i.e
> This is node n out of nnn nodes.
> In most of my database apps its common place to tell the user that they

are
> working on record x of xxx records I'm trying to provide the same
> functionality using the tree control.
>
>
> TIA
> meh
>
>



  Reply With Quote
Old 06-09-2003, 06:54 AM   #7
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
Default Re: Finding my position in a Treeview THX to all

Hello,

"Fergus Cooney" <filter-1@tesco.net> schrieb:
> ???


"THX" = "Thanks!"

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off