Please help; TreeView and CheckBox

M

MakeLei

Hi,
I have this code in below by cehrenre. It works well, but I have a question.
Given code is following from Parent to Children. I have a situation that I
have Parent - Child - GrandChild and even further.

What kind of modification should I make that it works the same way all the
way from Parent to Grandchildren - meaning that it will check all the boxex
automatically as parent is checked?

As you are able to notice I am a novice in programming ;o(

Thanks in advance
MakeLei
 
E

Ed

Here it is a code to checked all children nodes


Function CheckChildren(ByVal Node As Object, _
Optional isChecked As Boolean = True) As Node
'---------------------------------------------------------------------------------------
' Procedure : CheckChildren
' DateTime : 09/06/2006 10:16
' Author : Edward Pivnik
' Purpose : Check/Uncheked recursively all children nodes to the Node
'---------------------------------------------------------------------------------------
On Error GoTo HandleError

Dim nNode As Node

If Node.Children > 0 Then ' There are children.

' Set first child's node
Set nNode = Node.Child

If Node.Children = 1 Then ' Only one child node
nNode.Checked = isChecked
CheckChildren nNode, isChecked
Else
' While nNode is not the index of the child node's
' last sibling, get next sibling's node.
While nNode.Index <> Node.Child.LastSibling.Index

nNode.Checked = isChecked
' call itselft to iterate through children nodes
CheckChildren nNode, isChecked

' Reset to next sibling's node.
Set nNode = nNode.Next
nNode.Checked = isChecked
' call itselft to iterate through children nodes
CheckChildren nNode, isChecked
Wend
End If
End If


ExitHere:
Exit Function

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[Module].[Common ActiveX Control Operational].[CheckChildren]"
End Select
GoTo ExitHere

End Function


and here is a code to check all parents nodes. This is required if you
checked all children nodes on so parent node must be shown checked too

Function CheckParents(ByVal Node As Object) As Node
'---------------------------------------------------------------------------------------
' Procedure : CheckParents
' DateTime : 09/06/2006 10:49
' Author : Edward Pivnik
' Purpose : Check/Uncheked recursively all parents nodes to the Node
'---------------------------------------------------------------------------------------
'
On Error GoTo HandleError

Dim nNode As Node, n As Integer
Set nNode = Node.Parent

If Not nNode Is Nothing Then ' There is a parent node.

' Set first sibling's node
Set nNode = Node.FirstSibling
If nNode.Checked Then n = n + 1

' While nNode is not the index of the
' last sibling, get next sibling's node.
While nNode.Index <> Node.LastSibling.Index
' Reset to next sibling's node.
Set nNode = nNode.Next
If nNode.Checked Then n = n + 1
Wend

If n = Node.Parent.Children Then
Node.Parent.Checked = True
Else
Node.Parent.Checked = False
End If

' call itself to iterate through parent nodes
CheckParents Node.Parent

End If

ExitHere:
Exit Function

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[Module].[Common ActiveX Control Operational].[CheckParents]"
End Select
GoTo ExitHere

End Function
 
M

MakeLei via AccessMonster.com

Hi Ed and Others,
First of all thank You Ed for Your help so far.... ;o)
And now once again it is time for me to feel really stupid.

I copy Ed's code to VB to form page - OK
How do I fire them? I have this part:

Private Sub xTree_NodeCheck(ByVal Node As Object)

End Sub

I have tried all the different ways to fire Ed's code, but no success. Could
Someone please help me a little bit forward - I am learning...

Thanks in advance
MakeLei
Here it is a code to checked all children nodes

Function CheckChildren(ByVal Node As Object, _
Optional isChecked As Boolean = True) As Node
'---------------------------------------------------------------------------------------
' Procedure : CheckChildren
' DateTime : 09/06/2006 10:16
' Author : Edward Pivnik
' Purpose : Check/Uncheked recursively all children nodes to the Node
'---------------------------------------------------------------------------------------
On Error GoTo HandleError

Dim nNode As Node

If Node.Children > 0 Then ' There are children.

' Set first child's node
Set nNode = Node.Child

If Node.Children = 1 Then ' Only one child node
nNode.Checked = isChecked
CheckChildren nNode, isChecked
Else
' While nNode is not the index of the child node's
' last sibling, get next sibling's node.
While nNode.Index <> Node.Child.LastSibling.Index

nNode.Checked = isChecked
' call itselft to iterate through children nodes
CheckChildren nNode, isChecked

' Reset to next sibling's node.
Set nNode = nNode.Next
nNode.Checked = isChecked
' call itselft to iterate through children nodes
CheckChildren nNode, isChecked
Wend
End If
End If


ExitHere:
Exit Function

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[Module].[Common ActiveX Control Operational].[CheckChildren]"
End Select
GoTo ExitHere

End Function

and here is a code to check all parents nodes. This is required if you
checked all children nodes on so parent node must be shown checked too

Function CheckParents(ByVal Node As Object) As Node
'---------------------------------------------------------------------------------------
' Procedure : CheckParents
' DateTime : 09/06/2006 10:49
' Author : Edward Pivnik
' Purpose : Check/Uncheked recursively all parents nodes to the Node
'---------------------------------------------------------------------------------------
'
On Error GoTo HandleError

Dim nNode As Node, n As Integer
Set nNode = Node.Parent

If Not nNode Is Nothing Then ' There is a parent node.

' Set first sibling's node
Set nNode = Node.FirstSibling
If nNode.Checked Then n = n + 1

' While nNode is not the index of the
' last sibling, get next sibling's node.
While nNode.Index <> Node.LastSibling.Index
' Reset to next sibling's node.
Set nNode = nNode.Next
If nNode.Checked Then n = n + 1
Wend

If n = Node.Parent.Children Then
Node.Parent.Checked = True
Else
Node.Parent.Checked = False
End If

' call itself to iterate through parent nodes
CheckParents Node.Parent

End If

ExitHere:
Exit Function

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[Module].[Common ActiveX Control Operational].[CheckParents]"
End Select
GoTo ExitHere

End Function
Hi,
I have this code in below by cehrenre. It works well, but I have a question.
[quoted text clipped - 31 lines]
 
E

Ed

Here it is

Private Sub tvwTree_NodeCheck(ByVal Node As Object)
On Error GoTo HandleError

CheckChildren Node, Node.Checked
CheckParents Node

ExitHere:
Exit Sub

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[VBA Document].[Form_Trade].[tvwTree_NodeCheck]"
End Select
GoTo ExitHere
End Sub


also read next article http://www.devx.com/tips/Tip/13784 because treeview
control has bug with triggering NodeCheck events
--
Ed (MCP)


MakeLei via AccessMonster.com said:
Hi Ed and Others,
First of all thank You Ed for Your help so far.... ;o)
And now once again it is time for me to feel really stupid.

I copy Ed's code to VB to form page - OK
How do I fire them? I have this part:

Private Sub xTree_NodeCheck(ByVal Node As Object)

End Sub

I have tried all the different ways to fire Ed's code, but no success. Could
Someone please help me a little bit forward - I am learning...

Thanks in advance
MakeLei
Here it is a code to checked all children nodes

Function CheckChildren(ByVal Node As Object, _
Optional isChecked As Boolean = True) As Node
'---------------------------------------------------------------------------------------
' Procedure : CheckChildren
' DateTime : 09/06/2006 10:16
' Author : Edward Pivnik
' Purpose : Check/Uncheked recursively all children nodes to the Node
'---------------------------------------------------------------------------------------
On Error GoTo HandleError

Dim nNode As Node

If Node.Children > 0 Then ' There are children.

' Set first child's node
Set nNode = Node.Child

If Node.Children = 1 Then ' Only one child node
nNode.Checked = isChecked
CheckChildren nNode, isChecked
Else
' While nNode is not the index of the child node's
' last sibling, get next sibling's node.
While nNode.Index <> Node.Child.LastSibling.Index

nNode.Checked = isChecked
' call itselft to iterate through children nodes
CheckChildren nNode, isChecked

' Reset to next sibling's node.
Set nNode = nNode.Next
nNode.Checked = isChecked
' call itselft to iterate through children nodes
CheckChildren nNode, isChecked
Wend
End If
End If


ExitHere:
Exit Function

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[Module].[Common ActiveX Control Operational].[CheckChildren]"
End Select
GoTo ExitHere

End Function

and here is a code to check all parents nodes. This is required if you
checked all children nodes on so parent node must be shown checked too

Function CheckParents(ByVal Node As Object) As Node
'---------------------------------------------------------------------------------------
' Procedure : CheckParents
' DateTime : 09/06/2006 10:49
' Author : Edward Pivnik
' Purpose : Check/Uncheked recursively all parents nodes to the Node
'---------------------------------------------------------------------------------------
'
On Error GoTo HandleError

Dim nNode As Node, n As Integer
Set nNode = Node.Parent

If Not nNode Is Nothing Then ' There is a parent node.

' Set first sibling's node
Set nNode = Node.FirstSibling
If nNode.Checked Then n = n + 1

' While nNode is not the index of the
' last sibling, get next sibling's node.
While nNode.Index <> Node.LastSibling.Index
' Reset to next sibling's node.
Set nNode = nNode.Next
If nNode.Checked Then n = n + 1
Wend

If n = Node.Parent.Children Then
Node.Parent.Checked = True
Else
Node.Parent.Checked = False
End If

' call itself to iterate through parent nodes
CheckParents Node.Parent

End If

ExitHere:
Exit Function

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[Module].[Common ActiveX Control Operational].[CheckParents]"
End Select
GoTo ExitHere

End Function
Hi,
I have this code in below by cehrenre. It works well, but I have a question.
[quoted text clipped - 31 lines]
 
M

MakeLei via AccessMonster.com

Hi Ed,
Your made my great - thanks. I will read the article.

BR
Markku
Here it is

Private Sub tvwTree_NodeCheck(ByVal Node As Object)
On Error GoTo HandleError

CheckChildren Node, Node.Checked
CheckParents Node

ExitHere:
Exit Sub

HandleError:
Select Case Err.Number
Case Else
MsgBox "Error " & Err.Number & ": " & Err.Description,
vbCritical, "[VBA Document].[Form_Trade].[tvwTree_NodeCheck]"
End Select
GoTo ExitHere
End Sub

also read next article http://www.devx.com/tips/Tip/13784 because treeview
control has bug with triggering NodeCheck events
Hi Ed and Others,
First of all thank You Ed for Your help so far.... ;o)
[quoted text clipped - 126 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top