Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TreeView1.Nodes(1).relationship ??

Status
Not open for further replies.

markask

Programmer
Joined
Apr 28, 2005
Messages
167
Location
GB
Hi,

We can get some node's properties of a treeview, such as
TreeView1.Nodes(1).Text
but, how to get its relationship like
Print TreeView1.Nodes(1).relationship ??
 
No such property ...

You might want to investigate the Next, Previous, Parent, Child and Children properties
 
Thank you.
For example, I added two nodes by
TreeView1.Nodes.Add , , "RootKey", "RootText"
TreeView1.Nodes.Add "RootKey", tvwChild, "Key1", "Text1"
the relationship of Nodes(1) should be tvwChild or 4. How to get this? I don't think I can get it by its Next, Previous, Parent, Child or Children properties....
 
Try considering the idea that if a node has a parent then it must itself be a child of that parent
 
See this example:
TreeView1.Nodes.Add , , "RootKey", "RootText"
TreeView1.Nodes.Add "RootKey", tvwChild, "Key1", "Text1"
TreeView1.Nodes.Add "Key1", tvwNext, "Key2", "Text2"

Nodes(3) has a parent but its relationship property is tvwNext, instead of tvwChild.
 
So your third node isn't a child node as well, then? It could also be a previous if you added another next node ...

(perhaps you are beginning to see why there is no specific relationship property provided by the control; once a node is inserted it may have a number of relationships)
 
What exactly are you trying to do?

if you want to know if a Node is a child, As strongm mentioned, use the Parent Property...

Such as:
Code:
Function IsChild(ByVal Node As MSComctlLib.Node) As Boolean
  IsChild = (Not Node.Parent Is Nothing)
End Function

Function IsSibling(ByVal Node As MSComctlLib.Node) As Boolean
  IsSibling = ((Not Node.Next Is Nothing) or (Not Node.Previous Is Nothing))
End Function

Function IsParent(ByVal Node As MSComctlLib.Node) As Boolean
  IsParent = (Not Node.Children = 0)
End Function

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
I am sorry......but what I need is to save all informations of the treeview to a file!

If I can find relative and relationship of each node, the job becomes very easy:
[in the file]
1, "relative key1", "relationship1", "key1", "text1", ...
2, "relative key2", "relationship2", "key2", "text2", ...
3, ......
[next time I can display the treeview]
TreeView1.Nodes.Add "relative key1", "relationship1", "key1", "text1", ...
TreeView1.Nodes.Add "relative key2", "relationship2", "key2", "text2", ...

Otherwise, we need to do some recursive job to get informations for the treeview....
 
I'd go with the 'Otherwise', I'm afraid - a recursive job
 
The only REAL relationship nodes have are Parent relationship...

the others are just fancy methods to apply the Parent relationship...

Nodes are set up with a property pointing to their parent... (similar to a Linked List but in reverse)
Children are found by looping through all nodes to find references to the Parent...

Parent is determined by a node that has children.
Root is determined by A Node that has No parent.
Child is determined by a Node that has a Parent.
Sibling is determined by a Node that has the same parent as another Node...
Next is detrmined by a Sibling after a specified adjacent Node in the order created.
Previous is detrmined by a Sibling before a specified adjacent Node in the order created.

As far as saving to files, I would check into XML...
By nature, it has a tree file structure...
It is rather easy to set VB up to save a tree to XML and reload it later...
Code:
<XMLTree>
  <Node Name="Node1" Parent="" Text="Test 1">
    <Node Name="Node2" Parent="Node1" Text="Test 2">
      <Node Name="Node4" Parent="Node2" Text="Test 4">
        <Node Name="Node6" Parent="Node4" Text="Test 6">
      </Node>
      <Node Name="Node5" Parent="Node2" Text="Test 5">
      </Node>
    </Node>
    <Node Name="Node3" Parent="Node1" Text="Test 3">
      <Node Name="Node7" Parent="Node3" Text="Test 7">
        <Node Name="Node8" Parent="Node7" Text="Test 8">
      </Node>
    </Node>
  </Node>
</XMLTree>

A plain text file would work too...
You just need to recurse through and provide a link to each parent...

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
CubeE101,

Your idea is interesting......
Just don't know how to save the order of the nodes in the same level.
I mean in plain text file, don't know XML :(
 
They should be in order of creation...

Try something like:
Code:
Function SaveTree(Tree As MSComctlLib.TreeView) As String
  Dim Text As String
  Dim Node As MSComctlLib.Node
  For Each Node In Tree.Nodes
    If Not Node.Parent Is Nothing Then
      Text = Text & Node.Text & "|" & Node.Parent.Index & vbCrLf
    Else
      Text = Text & Node.Text & "|" & vbCrLf
    End If
  Next
  SaveTree = Text
End Function

Code:
Sub RestoreTree(Tree As MSComctlLib.TreeView, Text As String)
  Dim tNodes() As String
  Dim tNode() As String
  Dim i As Integer
  Tree.Nodes.Clear
  tNodes = Split(Text & vbCrLf, vbCrLf)
  For i = 0 To UBound(tNodes)
    If InStr(1, tNodes(i), "|") Then
      tNode = Split(tNodes(i), "|")
      If tNode(1) <> "" Then
        Tree.Nodes.Add Tree.Nodes(Val(tNode(1))), tvwChild, , tNode(0)
      Else
        Tree.Nodes.Add , , , tNode(0)
      End If
    End If
  Next
End Sub

To Save...
Code:
Open "file.txt" For Output As #1
  Print #1, SaveTree(TreeView1)
Close

To Restore:
Code:
Open "file.txt" For Input As #1
  RestoreTree TreeView1, Input(LOF(1), #1)
Close

Hope this Helps ;-)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Thank you for your help.
I will try that today.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top