我想将Treeview选择限制为最多8个子节点。我有一个以下代码取消选中该框,但在再次选中该框后立即。任何想法?。
感谢。
Private Sub TreeView1_NodeCheck(ByVal node As MSComctlLib.node)
If Not node.Parent Is Nothing Then
If node.Checked = True Then
couT = couT + 1
If couT <= 8 Then
'MsgBox node.Text & "_Checked !"
Else
node.Checked = False
End If
Else
couT = couT - 1
End If
End If
End Sub
答案 0 :(得分:0)
这个TreeView NodeCheck对象不像其他对象那样处理事件,设置.Checked = False
在Sub完成后没有任何意义。但是,在正常模块中使用Public变量和Sub,以及最重要的Application.OnTime
进行取消选中(在宏内完成时不会触发_NodeCheck
)
因此,在带有TreeView对象的UserForm模块中:
Private couT As Integer
Private Const MAX_NODES As Integer = 8
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
Dim bSchUncheckNode As Boolean
bSchUncheckNode = False
If Not Node.Parent Is Nothing Then
If Node.Checked = True Then
couT = couT + 1
If couT <= MAX_NODES Then
'Debug.Print Node.Text & "_Checked !"
Else
couT = couT - 1 ' Assuming the scheduled task completes successfully (or move the variable to standard module).
bSchUncheckNode = True
iNodeToUncheck = Node.Index
End If
Else
couT = couT - 1
End If
End If
' Schedule Sub call only if it should uncheck a node
If bSchUncheckNode Then Application.OnTime Now, "UncheckNode"
End Sub
Sub UserForm_Initialize()
couT = 0 ' Resets the counter to zero
'... Other things you have had
End Sub
在普通的vba模块中(相应地更改UserForm和TreeView对象的名称):
Public iNodeToUncheck As Integer
Sub UncheckNode()
' Assuming this is only be called by the UserForm scheduling
UserForm1.TreeView1.Nodes(iNodeToUncheck).Checked = False
End Sub