Results 1 to 5 of 5

Thread: Need help with Tree View Control

  1. #1
    hellswraith
    Guest

    Exclamation Need help with Tree View Control

    I need some help with the Tree View control. I set the properties so that the nodes have check boxes next to them.

    When the parent node is checked, I want all the child nodes of that parent to be checked. Same with unchecking them. I won't know for sure how many child nodes there are. How do I do this in code, and do I do it in the NodeCheck event?

    Some sample code would be a great help. Thanks.

  2. #2
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    Try this code...

    VB Code:
    1. Public Sub CheckNodes(MyTreeView As TreeView, ByVal Node As MSComctlLib.Node, bChecked As Boolean)
    2.     Dim iIndex As Integer
    3.     If Not Node Is Nothing Then
    4.         If Node.Children > 0 Then
    5.             iIndex = Node.Child.Index
    6.             Node.Child.Checked = bChecked
    7.             CheckNodes MyTreeView, Node.Child, bChecked
    8.             While iIndex <> Node.Child.LastSibling.Index
    9.                 MyTreeView.Nodes(iIndex).Next.Checked = bChecked
    10.                 iIndex = MyTreeView.Nodes(iIndex).Next.Index
    11.                 CheckNodes MyTreeView, MyTreeView.Nodes(iIndex).Next, bChecked
    12.             Wend
    13.             CheckNodes MyTreeView, Node.Child.Next, bChecked
    14.         Else
    15.             Node.Checked = bChecked
    16.         End If
    17.     End If
    18. End Sub


    Call from the TreeView_NodeCheck event like this
    VB Code:
    1. Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
    2.     CheckNodes TreeView1, Node, Node.Checked
    3. End Sub

    Greg
    Free VB Add-In - The Reference Librarian
    Click Here for screen shot and download link.

  3. #3
    hellswraith
    Guest
    Thanks a lot for your help, it works great.

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    hellswraith, be advised that MS acknowledges a bug in the TV control when checkboxes are used. They advise setting the Checkbox style of the TreeView by using API calls, rather than simply setting the "Checkboxes" property to True.

    In a module, you would need:
    Code:
        Public Const TVS_CHECKBOXES As Long = &H100
        Public Const GWL_STYLE      As Long = (-16)
    
        Public Declare Function GetWindowLong _
            Lib "user32" Alias "GetWindowLongA" _
            (ByVal hwnd As Long, _
             ByVal nIndex As Long) As Long
    
        Public Declare Function SetWindowLong _
            Lib "user32" Alias "SetWindowLongA" _
            (ByVal hwnd As Long, _
             ByVal nIndex As Long, _
             ByVal dwNewLong As Long) As Long
    In your form (probably in the Load event), you would need:
    Code:
        Dim lngCurStyle As Long
        Dim lngResult As Long
        ' === Set the Checkbox style of the TreeView ===
        ' As advised by Microsoft, due to a bug in the TreeView control,
        ' set the Checkbox style of the TreeView by using the following
        ' API calls, rather than simply setting the "Checkboxes" property
        ' to True ...
        lngCurStyle = GetWindowLong(tvwGlobalUpdate.hwnd, GWL_STYLE)
        lngResult = SetWindowLong(tvwGlobalUpdate.hwnd, GWL_STYLE, _
                                  lngCurStyle Or TVS_CHECKBOXES)
    Here's a link to the MS article:
    http://support.microsoft.com/support.../Q192/1/88.asp
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    hellswraith
    Guest
    Thanks, I would probably would have been beating my head trying to figure out what is wrong if something happened. Good lookin out for me. That is why this forum is as good as it is.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width