PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Finding my position in a Treeview
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Finding my position in a Treeview
![]() |
Finding my position in a Treeview |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
"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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 > > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#5 |
|
Guest
Posts: n/a
|
"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 |
|
|
|
#6 |
|
Guest
Posts: n/a
|
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 > > |
|
|
|
#7 |
|
Guest
Posts: n/a
|
Hello,
"Fergus Cooney" <filter-1@tesco.net> schrieb: > ??? "THX" = "Thanks!" ;-) -- Herfried K. Wagner MVP · VB Classic, VB.NET http://www.mvps.org/dotnet |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

