如何根据父节点更新TreeView子节点属性

How to update TreeView child node based on parent node property

在 TreeView 中,如何仅在父节点的 Image 为 4 且任何子节点为 3 时才执行代码?

If TreeView2.Nodes(ii).Image = 4 And TreeView2.Nodes(ii).Image = 3 Then.

出于某种原因,这个 If TreeView2.Nodes(ii).Image = 4 检查父子,我不知道为什么。

我正在尝试使用 If TreeView2.Nodes(ii).parent.child.image=3,但它无法正常工作。

TreeView2.Nodes(ii).parent 永远是 Nothing。 TreeView 中的 top-level 节点没有 parent。当您使用 TreeView2.Nodes(ii).parent.child 时,您肯定会遇到错误。使用 parent.child 只会用于获取 parent 节点的第一个 child,这可能不是您想要做的。

听起来您想执行以下操作:

Dim objRootNode As Node
Dim objChildNode As Node
Dim iRootCounter As Integer
Dim iChildCounter As Integer

For iRootCounter = 1 To TreeView1.Nodes.Count

    Set objRootNode = TreeView1.Nodes(iRootCounter)

    If objRootNode.Image = 4 Then

        Set objChildNode = objRootNode.Child ' Gets first child

        For iChildCounter = 1 To objRootNode.Children

            If objChildNode.Image = 3 Then
                ' Write your code here
            End If

            Set objChildNode = objChildNode.Next ' Get next node

        Next

    End If

Next

Write your code here 处添加您的代码。这是 parent 有 Image = 4 和 child 有 Image = 3.

的地方