473,399 Members | 4,177 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,399 software developers and data experts.

Treeview Headache

Hi,

Is there a simple way to find the parent nodes under a given node.

For example I select Node Test, which is a parent, it has 3 children, and
two of those are also parents, and in turn one of those children is also a
parent.

What I want to do is to find out from the node that is selected :-

1. Its key, this is easy

2 for each child of this node that is also a parent what its key is, but
only for nodes that have children.

I know what I need to do in on paper, it is something like this

With selected node, get key convert to int store in array, along with the
name

For each node that is a parent node within the selected node do the same,
get key and name store in array.

Should be simple right? but my brain is melting

here is the code I am using

Sub CopyAllRecords(ByVal sK As String, ByVal ID As Long, ByVal PID As Long)

Dim n As Long

Dim ndeCopy As ComctlLib.Node

Dim arParentFields() As Integer

Dim blChildIsParent as Boolean

Dim iSelNodeChildCount as Integer

ReDim arParentFields(20)

If tvMain.Nodes(sK).Children > 0 Then

Set ndeCopy = tvMain.Nodes(sK).Child

Do While iSelNodeChildCount > 0 And blChildIsParent = True

iSelNodeChildCount = ndeCopy.Children

For n = 1 To iSelNodeChildCount

if nIDCount = UBound(arParentFields) Then

ReDim Preserve arParentFields(UBound(arParentFields) + 20 )

End If

arParentFields(nIDCount) = stripK(ndeCopy.Key)

nIDCount = nIDCount + 1

If n < tvMain.Nodes(sK).Children Then Set ndeCopy = ndeCopy.Next

Next n

iSelNodeChildCount = iSelNodeChildCount - 1

Loop

End If

This does not work, Instead of only processing the nodes under just the
selected node, it does this and every node on the same level as the selected
node.

I hope this is simple, but my head hurts now, can someone put me out of my
misery please

Thanks in advance

Rich
Jul 17 '05 #1
2 4446

"Rich" <so******@someplace.com> wrote in message
news:c7**********@sparta.btinternet.com...
Hi,

Is there a simple way to find the parent nodes under a given node.

For example I select Node Test, which is a parent, it has 3 children, and two of those are also parents, and in turn one of those children is also a parent.

What I want to do is to find out from the node that is selected :-

1. Its key, this is easy

2 for each child of this node that is also a parent what its key is, but only for nodes that have children.


You need two things: a recursive sub, and the use of Child and Next.
Something like this:

Private Sub Command1_Click()
If Not TreeView1.SelectedItem Is Nothing Then
Call TestNode(TreeView1.SelectedItem)
End If
End Sub

Private Sub TestNode(Parent As Node)
Dim oChild As Node

If Parent.Children > 0 Then

Debug.Print "Node " & Parent.Key & " has kids."

'get first child of Parent
Set oChild = Parent.Child

While Not oChild Is Nothing
'test the child node (recursive call)
Call TestNode(oChild)
'get next sibling of oChild, which is
'the next child of Parent
Set oChild = oChild.Next
Wend

End If

End Sub


Jul 17 '05 #2
> You need two things: a recursive sub, and the use of Child and Next.
Something like this:
:) Exactly like this
Private Sub Command1_Click()
If Not TreeView1.SelectedItem Is Nothing Then
Call TestNode(TreeView1.SelectedItem)
End If
End Sub

Private Sub TestNode(Parent As Node)
Dim oChild As Node

If Parent.Children > 0 Then

Debug.Print "Node " & Parent.Key & " has kids."

'get first child of Parent
Set oChild = Parent.Child

While Not oChild Is Nothing
'test the child node (recursive call)
Call TestNode(oChild)
'get next sibling of oChild, which is
'the next child of Parent
Set oChild = oChild.Next
Wend

End If

End Sub


Thanks alot for this Steve :) this has helped me out no end.

I have been struggling with this for 4 days now, I was trying to figure this
out on paper, looking at selected node and how many children it had so I set
up a for loop that would handle the initial children under the selected node
and then a while loop to handle the children of the children, I did not even
think of recursion, an obvious choice now, but at the time it was nowhere in
mind :(, I also just noticed that I was never setting the blChildIsParent to
false either.

Again thanks for this

Rich
Jul 17 '05 #3

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

Similar topics

0
by: Plumer | last post by:
Hello everyone, Yesterday I posted a message about implementing drag & drop in a TreeView control. I'm having real difficulty getting this to work -- the process seems to be incredibly...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
5
by: SoKool | last post by:
Can anyone point me to a site where I can get a free treeview control to use in ASP .NET or any tutorial that can help me build my own treeview control. I intend to use a treeview to generate a...
1
by: Shailendra Batham | last post by:
Hey guys I am using the IE Web controls treeview control. I popuplate the treeview with XML and it looks fine........... my question is how can I add a right click popup menu to the treeview...
3
by: Peter | last post by:
Hello, We are inserting a side menu to our application using a class that is writing HTML on all our pages. This is a part of the code as an example: writer.Write(" <table WIDTH=""100%""...
6
by: L.M | last post by:
Hello, I knew how to use the treeview under VB6. After migrating to .NET, well, I'm lost. I try to add a new node, either to the same level or as a child to a selected node in the treeview....
14
by: Mr.D | last post by:
How do I save/load the contents of a Treeview to a file? I have found several good examples written i VB6, but not a single one for VB.NET. Please help. ---- Tim
3
by: christof | last post by:
I've got a really easy problem, please help me: There are two pages in one I'm creating a TreeView like that: TreeView dbTree = new TreeView(); TreeNode dbTreeRoot = new TreeNode("Root");...
8
by: Matt MacDonald | last post by:
Hi All, I have a form that displays hierarchical categories in a treeview. Ok so far so good. What I was to do is have users be able to select a node in the treeview as part of filling out the...
7
by: John Rogers | last post by:
I had given up on this for a while, but I am attempting it again. This is what i am using to store my treeview layout into a db, I am only using a few fields to make it easier to understand while...
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: 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
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.