Click here to Skip to main content
15,945,119 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having problems coding the fallowing :
If somebody checks a child node in the treeview control, I want the parent node of the that child node to be checked.

Some thing like:

VB
For each node as treenode in nodes
    If childrennode.checked = true then
        Parentnode.check = true
    End if
Next



The following code loads XML info to my treeview control. Each node in the treeview has a checkbox.
VB
For Each Record As XElement In doc...<record>
            recordNode = TreeView1.Nodes.Add(Record.Name.ToString)

            For Each Leader As XElement In Record...<leader>

                leaderNode = recordNode.Nodes.Add(Leader.Name.ToString)

                For Each Controlfield As XElement In Record...<controlfield>
                    controlfieldNode = recordNode.Nodes.Add(Controlfield.@tag.ToString)
                Next
            Next
        Next

        For Each Record As XElement In doc...<record>
            recordNode = TreeView1.TopNode
            For Each Datafield As XElement In Record...<datafield>
                datafielNode = recordNode.Nodes.Add(Datafield.@tag.ToString)
                For Each Subfield As XElement In Datafield...<subfield>
                    datafielNode.Nodes.Add(Subfield.@code.ToString)
                Next
            Next
        Next
Posted
Updated 13-May-11 6:58am
v4
Comments
Fabio V Silva 13-May-11 13:08pm    
What are the problems you're having then?
stopete82 13-May-11 13:17pm    
Syntax problem:

Dim nodes As TreeNodeCollection

For each node as treenode in nodes
If childrennode.checked = True Then
Parentnode.check = True
End If
Next
with this code I get childrennode not declared and parentnode change to Treenode.

1 solution

Every node has a Parent property. This is trivial to do.

All you do is
checkedNode.Parent.Checked = True

where checkedNode is the node the user checked. You can get this from the eventargs that are passed to you code depending on which TreeView event you're using.
 
Share this answer
 
Comments
stopete82 13-May-11 14:43pm    
Thanks for your help, this is the code that worked for me:

Private Sub TreeView1_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck



Dim Pnode As TreeNode = e.Node


Dim PPnode As TreeNode = Pnode.Parent


If Pnode.Checked = True Then
While PPnode IsNot Nothing
RemoveHandler PPnode.TreeView.AfterCheck, AddressOf TreeView1_AfterCheck
PPnode.Checked = True
AddHandler PPnode.TreeView.AfterCheck, AddressOf TreeView1_AfterCheck
PPnode = PPnode.Parent
End While
Else
UncheckParent(PPnode)
End If

End Sub

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900