查找结点示例

在指定结点找文本值为 s 的结点,如果没有,则创建。

Function fnd(tv As TreeView, par As Node, s As String) As Node
    Dim nd As Node
    If par Is Nothing Then
        If tv.Nodes.Count = 0 Then
            Set nd = Nothing
        Else
            Set nd = tv.Nodes(1)
        End If
    Else
        Set nd = par.Child
    End If

    Do While Not (nd Is Nothing)
        If nd.Text = s Then
            Set fnd = nd
            Exit Function
        End If
        Set nd = nd.Next
    Loop

    If par Is Nothing Then
        Set fnd = tv.Nodes.Add(, tvwChild, , s)
    Else
        Set fnd = tv.Nodes.Add(par, tvwChild, , s)
        par.Expanded = True
    End If
End Function
参数 描述
tv TreeView 对象
par 父结点 Node 对象,如为空,则查找根结点
s 子结点的文本值
返回值 找到或新增的结点 Node 对象