Setting Checked On NodeCheck Event

后端 未结 2 1781
遇见更好的自我
遇见更好的自我 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:04

    I've encountered this issue, and discovered a workaround.

    You can't set the 'checked-ness' of a treeview's checkbox in its own NodeCheck event.

    However, assuming you're capturing the user clicking the checkbox, the Treeview_Click event fires immediately afterwards (as they had to click to change it), and you can set checkboxes in that event.

    So all you need to do is store a reference to the checkbox you need to be changed and set it in the Click event, before clearing the reference.

    Private WithEvents tv As TreeView
    
    Private checkBoxToSet_Node As Node
    Private checkBoxToSet_value As Boolean
    
    
    Private Sub tv_NodeCheck(ByVal Node As MSComctlLib.Node)
    
        Dim prompt As VbMsgBoxResult
    
        prompt = MsgBox("Do you REALLY want to set this checkbox?", vbQuestion + vbYesNoCancel)
    
        ' can't set the checked-ness here, so we store it
    
        If (prompt <> vbYes) Then
            Set checkBoxToSet_Node = Node
            checkBoxToSet_value = Not Node.checked
        End If
    
    End Sub
    
    
    Private Sub tv_Click()
    
        ' check if we had previously set a node to have its checkbox changed
    
        If (Not checkBoxToSet_Node Is Nothing) Then
            tv.Nodes(checkBoxToSet_Node.key).checked = checkBoxToSet_value
    
            Set checkBoxToSet_Node = Nothing
        End If
    
    End Sub
    

提交回复
热议问题