473,396 Members | 2,089 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,396 software developers and data experts.

Finding my position in a Treeview

meh
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
Nov 20 '05 #1
6 14159
"meh" <ho*****@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
Nov 20 '05 #2
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" <ho*****@qwest.net> wrote in message
news:es**************@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

Nov 20 '05 #3
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
Nov 20 '05 #4
"Herfried K. Wagner [MVP]" <hi*******@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
Nov 20 '05 #5
meh
THX
"meh" <ho*****@qwest.net> wrote in message
news:es**************@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

Nov 20 '05 #6
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
???


"THX" = "Thanks!"

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #7

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

Similar topics

3
by: Nick Wedd | last post by:
I have this <DIV id="canopy" onClick="myhandler(event.x,event.y); return false;"> <IMG border=0 height=66 width=292 src="gifs/top.gif"> </DIV> which works in IE. myhandler gets called with the...
6
by: Vern | last post by:
I'm adding twenty nodes (Parent and child) to a tree view. Only 10 items are viewable in the window at at time. So when it initially shows the tree, it shows the last ten items forcing me to use...
14
by: Evan Kontos | last post by:
I am trying to implement a Treeview w/an XML file and I even copied and pasted examples from MSDN but can't get them to work. Any suggestions welcome. XML File <TREENODES> <TREENODE...
7
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
1
by: Elmo Watson | last post by:
forgive me, but I'm fairly new to VB.Net - having only used VB6 in the past - - I can easily place a node on the treeview (tv1.nodes.add(key, text) BUT - in VB6, we'd add a child node...
1
LegalIT
by: LegalIT | last post by:
Hello, I have an application that loads a set of database records into memory. I then have buttons to move to the first, next, previous or last record. I would like to add a horizontal scroll...
2
jhardman
by: jhardman | last post by:
I am trying to draw a connector line from a fixed point to a control that will move depending on contents or if the window is resized. Is there a convenient way to find a control's x,y coordinates...
98
by: scienceman88 | last post by:
I'm making a prime finder another thing I'd like is a loop to check all possible odd factors if I can do that i can find all primes under 18 quintillion I think and store them in files ( yes I have...
2
by: Wedders | last post by:
Hi all, I'm using the function provided by: http://www.quirksmode.org/js/findpos.html to find the position of a box on the screen, where I then fade-in a speech bubble with some data. This...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.