Search code examples
vbams-accessvb6treeviewms-access-2003

Setting Checked On NodeCheck Event


Setting the checked property during the NodeCheck event has been causing it to revert to its previous state.

For example:The node is checked, and the event below gets fired. It finds that the node is checked and sets it to be false. If I walk through the code with a break, the node will reflect this in the user interface. Although, as soon as the code hits the end sub, the checkbox will jump back to being set to true.

Private Sub treeviewExample_NodeCheck(ByVal Node As Object)
     If Node.Checked = True Then
            Node.Checked = False
     ElseIf Node.Checked = False Then
            Node.Checked = True
     End If
end sub

How do I set the checked property during the NodeCheck event?

I have tried the solution here that sets the node to a local or global variable and then sets it, and it does the same thing.


Solution

  • You can leave the Checkboxes property False and use the Windows API to set the checkboxes property. Then use the NodeClick event to choose whether to check or uncheck the node.

    Option Explicit
    
    Private Const TVS_CHECKBOXES As Long = &H100
    Private Const GWL_STYLE As Long = (-16)
    Private Const TVS_HASLINES As Long = 2
    Private Const TV_FIRST As Long = &H1100
    Private Const TVM_SETBKCOLOR As Long = (TV_FIRST + 29)
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    
    Private Sub Form_Load()
    
        SetTVCheckboxStyle TreeView1
    
    End Sub
    
    Private Sub SetTVCheckboxStyle(pobjTV As TreeView)
    
        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(pobjTV.hwnd, GWL_STYLE)
        lngResult = SetWindowLong(pobjTV.hwnd, GWL_STYLE, _
                                  lngCurStyle Or TVS_CHECKBOXES)
    
    End Sub
    

    As you add your node set some property of the nodes you want disabled so you can check the property later. I chose to use the ForeColor property so the disabled nodes would have a disabled appearance. Then use the NodeClick event to check, clear, or ignore the user clicks.

    Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    
        If Node.ForeColor <> vbGrayText Then
            Node.Checked = Not Node.Checked
        End If
    
    End Sub