Help with NodeCheck event of Treeview Control

Prysson

Registered User.
Local time
Today, 22:51
Joined
Sep 23, 2002
Messages
45
Dealing with a MS Treeview 6.0 with the property set to use check boxes. Normally this process only check the node selected and ignores the parent child relationships

I have managed to get some code that automaticaly checks parent nodes when a node is checked and unchecks child nodes if the parent is unchecked...

I have however run into a problem...

The code checks or unchecks the node on the Mouse Down but it doesnt update the parent/child relationships until the Mouse Up

Not sure why it does this since the code inst specific to mouse down or mouse up events.

Regardless it is doing it and the problem is that if the mouse accidentally moves off of the check box before the Mouse Up then the update doesnt occur and the node gets "abandoned"


Anyone have anyideas on how to resolve this

Here is the code for the NodeCheck

Private Sub TreeViewLocation_NodeCheck(ByVal Node As Object)
On Error Resume Next ' In case there are no parents or parent.parents
Dim NodX As Node, NodY As Node, NodZ As Node, strNodeKey As String

If Node.Checked Then
Node.Parent.Checked = True
Node.Parent.Parent.Checked = True
Else
strNodeKey = Node.Key
For Each NodX In Me!TreeViewLocation.Nodes
Set NodY = NodX.Parent
Set NodZ = NodY.Parent
If Not NodY Is Nothing Then
If NodY.Key = strNodeKey Then NodX.Checked = False
End If
If Not NodZ Is Nothing Then
If NodZ.Key = strNodeKey Then NodX.Checked = False
End If
Next NodX
End If
End Sub



Just an FYI in my tree view there are three nodes.
 
I came up with this example, which works ok for me...

Code:
Private Sub Form_Load()
  TreeView1.Checkboxes = True
  With TreeView1.Nodes
    .Add , , "n1", "n1"
    .Add "n1", tvwChild, "n1_1", "n1_1"
    .Add "n1", tvwChild, "n1_2", "n1_2"
    .Add "n1", tvwChild, "n1_3", "n1_3"
    .Add "n1_1", tvwChild, "n1_1_1", "n1_1_1"
    .Add "n1_1", tvwChild, "n1_1_2", "n1_1_2"
    .Add "n1_2", tvwChild, "n1_2_1", "n1_2_1"
  End With
End Sub

Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
  PropagateChecks Node
End Sub

Private Sub PropagateChecks(ByVal ParentNode As MSComctlLib.Node)
  Dim oNode As MSComctlLib.Node
  Dim lNodeIndex As Long
  If ParentNode.Children > 0 Then
    Set oNode = ParentNode.Child
    oNode.Checked = ParentNode.Checked
    Call PropagateChecks(oNode)
    For lNodeIndex = 1 To ParentNode.Children - 1
      Set oNode = oNode.Next
      oNode.Checked = ParentNode.Checked
      Call PropagateChecks(oNode)
    Next
  End If
  Set oNode = Nothing
End Sub

The PropagateChecks function is called recursively to make sure that checks are propagated down any number of levels.
 

Users who are viewing this thread

Back
Top Bottom