设置Checked On NodeCheck事件 [英] Setting Checked On NodeCheck Event

查看:251
本文介绍了设置Checked On NodeCheck事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NodeCheck事件期间设置选中的属性已导致其恢复到先前的状态.

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

例如:检查节点,并触发以下事件.它发现该节点已检查,并将其设置为false.如果我稍稍休息一下代码,该节点将在用户界面中反映出来.虽然,只要代码到达end sub,复选框就会跳回设置为true.

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

如何在NodeCheck事件期间设置选中的属性?

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.

推荐答案

您可以将Checkboxes属性保留为False,并使用Windows API设置checkboxes属性.然后使用NodeClick事件选择是选中还是取消选中该节点.

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

添加节点时,请设置要禁用的节点的某些属性,以便以后可以检查该属性.我选择使用ForeColor属性,因此禁用的节点将具有禁用的外观.然后使用NodeClick事件检查,清除或忽略用户的点击.

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

这篇关于设置Checked On NodeCheck事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆