Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 341
  • Last Modified:

treeview node search

I need a function to get a treeview's node's children's indexes.  ex what ever node I have highlighted in a treeview, i need to be able to check mark all nodes under it and their children and theirs etc. the treeview is a MS treeview with the checkbox property set to true.  any function will do, vbonly or api. Thanks in advance
0
jrspano
Asked:
jrspano
  • 2
1 Solution
 
Anthony PerkinsCommented:
Using VB (I would think that there would have to be a faster way using API) this seems to work for me, but you may need to double check me.

Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
 
CheckNodes Node, Node.Checked

End Sub

Sub CheckNodes(ByVal Node As MSComctlLib.Node, ByVal Check As Boolean)
Dim I As Integer
Dim nodChild As Node

If Node.Children Then
   Set nodChild = Node.Child
End If
For I = 1 To Node.Children
   nodChild.Checked = Check
   CheckNodes nodChild, Check
   Set nodChild = nodChild.Next
Next

End Sub
0
 
adgCommented:
ping
0
 
Anthony PerkinsCommented:
The following code may be a bit more efficient:

Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)

If Node.Children Then
   CheckNodes Node, Node.Checked
End If

End Sub

Sub CheckNodes(ByVal Node As MSComctlLib.Node, ByVal Check As Boolean)
Dim I As Integer
Dim nodChild As Node
Dim intChildren as Integer

Set nodChild = Node.Child
intChildren = Node.Children
For I = 1 To intChildren
  nodChild.Checked = Check
  If nodChild.Children Then
     CheckNodes nodChild, Check
  End If
  If I < intChildren Then  'The last one will not have a next sibling
     Set nodChild = nodChild.Next
  End If
Next

Set nodChild = Nothing

End Sub

You should also note there is a bug with the NodeCheck event.  Here is the MSDN article that describes a workaround:
BUG: TreeView: NodeCheck Event Does Not Occur (Q221557)
0
 
jrspanoAuthor Commented:
thanks worked great
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now