Setting Checked On NodeCheck Event

后端 未结 2 1759
遇见更好的自我
遇见更好的自我 2021-01-26 02:08

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

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-26 03:15

    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
    

提交回复
热议问题