首页  编辑  

TreeView查找下一个节点

Tags: /C#/界面处理/ListView_TreeView/   Date Created:

Private Function GetNextNode( ByVal ATreeNode) As TreeNode

    If ATreeNode Is Nothing Then

        Return Nothing

    End If

    If ATreeNode.Nodes.Count > 0 Then

        Return ATreeNode.Nodes( 0 )

    Else

        While True

            If ATreeNode.Parent Is Nothing Then

                If ATreeNode.Index + 1 < ATreeNode.TreeView.Nodes.Count Then

                    Return ATreeNode.TreeView.Nodes(ATreeNode.Index + 1 )

                Else

                    Return Nothing

                End If

            Else

                If ATreeNode.Index + 1 < ATreeNode.Parent.Nodes.Count Then

                    Return ATreeNode.Parent.Nodes(ATreeNode.Index + 1 )

                Else

                   ATreeNode = ATreeNode.Parent

                End If

            End If

        End While

        Return Nothing

    End If

End Function

Private Sub FindNextNode( ByVal ATreeView As TreeView , ByVal AText As String)

    If ATreeView Is Nothing Or ATreeView.Nodes.Count <= 0 Then

        Return

    End If

    Dim vTreeNode As TreeNode = ATreeView.SelectedNode

    If vTreeNode Is Nothing Then

       vTreeNode = ATreeView.Nodes( 0 )

    Else

       vTreeNode = GetNextNode(vTreeNode)

    End If

    Dim vStartNode As TreeNode = vTreeNode

    While Not vTreeNode Is Nothing

        If vTreeNode.Text.IndexOf(AText) >= 0 Then

           ATreeView.SelectedNode = vTreeNode

            Return

        End If

       vTreeNode = GetNextNode(vTreeNode)

    End While

   ATreeView.SelectedNode = Nothing

End Sub

Private Sub Button1_Click( ByVal sender As System.Object , _

    ByVal e As System.EventArgs) Handles Button1.Click

   FindNextNode(TreeView1, " )

   TreeView1.HideSelection = False

    If Not TreeView1.SelectedNode Is Nothing Then

        If TreeView1.SelectedNode.IsVisible Then

           TreeView1.SelectedNode.EnsureVisible()

        End If

    End If

End Sub