2014-06-18 139 views
0

我想检查我的树视图的根是否被检查。我不在乎孩子是否被检查。检查根是否被检查Treeview VBA

这是我的代码:

If (TreeView.Nodes.Count > 0) Then 
Dim check As Boolean 
check = False 
Set nd = TreeView.Nodes(1).Root.FirstSibling 
    Do Until nd Is Nothing 
     If nd.Parent Is Nothing And (nd.Selected) Then 
      check = True 
      MsgBox " a root is checked !" 
     End If 
     Set nd = nd.Next 
    Loop 
End If 
If Not check Then 
     MsgBox "No root checked !" 
End If 

当没有选中任何代码工作。但是,当一个孩子检查它告诉我,根检查... 我真的不知道如何改变这一点!

+0

如果nd.Parent一无所有,(nd.Checked)那么问题就来自这里 – tbille

回答

0

您检查.Selected代替.Checked所以更改为:

If nd.Parent Is Nothing And (nd.Checked) Then 

或者怎么样:

Dim nd As Node, isCheckedRoot As Boolean 
For Each nd In TreeView.Nodes 
    If nd.Parent Is Nothing And nd.Checked Then 
     isCheckedRoot = True 
     Exit For 
    End If 
Next 

MsgBox IIf(isCheckedRoot, "a root is", "No root") & " Checked"