vba - 在NodeCheck事件上,已经选中vba设置

  显示原文与译文双语对照的内容

NodeCheck 事件期间设置checked属性已经导致它恢复到以前的状态。

检查 example:The node,然后触发下面的事件。 它发现 node 被选中,并将它的设置为 false 。 如果我用break遍历代码,node 将会在用户界面中反映这个问题。 尽管代码到达 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 事件中设置checked属性?

我已经尝试了这个解决方案 ,它将设置为本地或者全局变量,然后设置它,然后执行相同的操作。

时间: 原作者:

你可以保留复选框属性 false,并使用 Windows API设置复选框属性。 然后使用NodeClick事件选择是否选中或者取消选中 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



添加 node 时,将禁用要禁用的节点的某些属性,以便以后检查属性。 我选择使用ForeColor属性,使禁用的节点具有禁用的外观。 然后使用NodeClick事件检查。清除或者忽略用户单击。


Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)



 If Node.ForeColor <> vbGrayText Then


 Node.Checked = Not Node.Checked


 End If



End Sub



原作者:
...