Treeview - Identify top level node

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xla76

    #1

    Treeview - Identify top level node

    I have a simple treeview (treeview1) to which I have added two nodes
    (nodeA and nodeB) which have n levels of child nodes.

    What I want is to be able to identify whether the child node I select
    is under NodeA or NodeB. I can do this by splitting the
    selectednode.fu llpath of the child node but there must be a better
    way.

    Can any kind soul can enlighten me?

    Cheers
    Pete

  • Arthur Dzhelali

    #2
    Re: Treeview - Identify top level node

    If I got your question right.
    You need to find Parent node
    here you go:

    dim pNode as TreeNode
    if SelectedNode.Pa rent isNot Nothing then
    pNode=SelectedN ode.Parent
    ' Now once you got your parent node you can use tag/text/name properties
    ' or any other means to determine if this is NodeA or NodeB

    end if


    xla76 <pete.forman@gm ail.comwrote in news:1183983720 .731689.205710
    @d55g2000hsg.go oglegroups.com:
    I have a simple treeview (treeview1) to which I have added two nodes
    (nodeA and nodeB) which have n levels of child nodes.
    >
    What I want is to be able to identify whether the child node I select
    is under NodeA or NodeB. I can do this by splitting the
    selectednode.fu llpath of the child node but there must be a better
    way.
    >
    Can any kind soul can enlighten me?
    >
    Cheers
    Pete
    >
    >

    Comment

    • rowe_newsgroups

      #3
      Re: Treeview - Identify top level node

      On Jul 9, 1:00 pm, Arthur Dzhelali <a.dzhel...@the day.comwrote:
      If I got your question right.
      You need to find Parent node
      here you go:
      >
      dim pNode as TreeNode
      if SelectedNode.Pa rent isNot Nothing then
      pNode=SelectedN ode.Parent
      ' Now once you got your parent node you can use tag/text/name properties
      ' or any other means to determine if this is NodeA or NodeB
      >
      end if
      >
      xla76 <pete.for...@gm ail.comwrote in news:1183983720 .731689.205710
      @d55g2000hsg.go oglegroups.com:
      >
      I have a simple treeview (treeview1) to which I have added two nodes
      (nodeA and nodeB) which have n levels of child nodes.
      >
      What I want is to be able to identify whether the child node I select
      is under NodeA or NodeB. I can do this by splitting the
      selectednode.fu llpath of the child node but there must be a better
      way.
      >
      Can any kind soul can enlighten me?
      >
      Cheers
      Pete
      ' Now once you got your parent node you can use tag/text/name properties
      ' or any other means to determine if this is NodeA or NodeB
      Or just use "if pNode is NodeA then..."

      Thanks,

      Seth Rowe

      Comment

      • Heikki Leivo

        #4
        Re: Treeview - Identify top level node

        dim pNode as TreeNode
        if SelectedNode.Pa rent isNot Nothing then
        pNode=SelectedN ode.Parent
        ' Now once you got your parent node you can use tag/text/name properties
        ' or any other means to determine if this is NodeA or NodeB
        >
        end if
        Or maybe even better:

        Function IsChildNode(Chi ldNode as TreeNode, ParentCandidate as TreeNode) As
        Boolean
        Dim parentNode As TreeNode = ChildNode.Paren t
        Do Until parentNode Is Nothing
        If parentNode Is ParentCandidate Then Return True
        'Or: If parentNode.Equa ls(ParentCandid ate) Then Return True
        parentNode = parentNode.Pare nt
        Loop
        Return False
        End Function


        Comment

        • xla76

          #5
          Re: Treeview - Identify top level node

          Still not working, I always get nodeA as the result, even when
          selecting a nested child node of nodeB.

          ' Created as follows...

          Dim NodeA As TreeNode
          NodeA = New TreeNode("myNod eA")
          Dim NodeB As TreeNode
          NodeB = New TreeNode("myNod eB")
          TreeView1.Nodes .Add(NodeA)
          TreeView1.Nodes .Add(NodeB)

          Have I done something wrong?

          Comment

          • rowe_newsgroups

            #6
            Re: Treeview - Identify top level node

            On Jul 9, 2:14 pm, xla76 <pete.for...@gm ail.comwrote:
            Still not working, I always get nodeA as the result, even when
            selecting a nested child node of nodeB.
            >
            ' Created as follows...
            >
            Dim NodeA As TreeNode
            NodeA = New TreeNode("myNod eA")
            Dim NodeB As TreeNode
            NodeB = New TreeNode("myNod eB")
            TreeView1.Nodes .Add(NodeA)
            TreeView1.Nodes .Add(NodeB)
            >
            Have I done something wrong?
            Here's a complete project that should work (note it will fire when you
            click the [+] or [-] buttons giving the wrong node text). Just copy
            this into a new form in your project and press F5 to run it.

            Public Class Form1

            Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
            System.EventArg s) Handles MyBase.Load
            Dim treeView As New TreeView()
            Dim nodeA As New TreeNode("NodeA ")
            Dim nodeB As New TreeNode("NodeB ")
            For i As Integer = 1 To 5
            nodeA.Nodes.Add (String.Format( "NodeA child {0}", i))
            nodeB.Nodes.Add (String.Format( "NodeB child {0}", i))
            Next
            treeView.Nodes. Add(nodeA)
            treeView.Nodes. Add(nodeB)

            treeView.Dock = DockStyle.Fill
            treeView.Expand All()
            AddHandler treeView.NodeMo useClick, AddressOf
            treeview_NodeMo useClick
            Me.Controls.Add (treeView)

            End Sub

            Private Sub treeview_NodeMo useClick(ByVal sender As Object, ByVal
            e As TreeNodeMouseCl ickEventArgs)
            Dim node As TreeNode = DirectCast(send er,
            TreeView).Selec tedNode

            If node.Parent Is Nothing Then
            MsgBox(String.F ormat("You clicked a parent node named
            {0}", node.Text))
            Else
            Dim parent As TreeNode = node.Parent
            While Not parent.Parent Is Nothing
            parent = parent.Parent
            End While
            MsgBox(String.F ormat("The node's parent is {0}",
            parent.Text))
            End If
            End Sub

            End Class

            Hope that helps!

            Thanks,

            Seth Rowe

            Comment

            • xla76

              #7
              Re: Treeview - Identify top level node

              On Jul 9, 7:37 pm, rowe_newsgroups <rowe_em...@yah oo.comwrote:
              On Jul 9, 2:14 pm, xla76 <pete.for...@gm ail.comwrote:
              >
              Still not working, I always get nodeA as the result, even when
              selecting a nested child node of nodeB.
              >
              ' Created as follows...
              >
              Dim NodeA As TreeNode
              NodeA = New TreeNode("myNod eA")
              Dim NodeB As TreeNode
              NodeB = New TreeNode("myNod eB")
              TreeView1.Nodes .Add(NodeA)
              TreeView1.Nodes .Add(NodeB)
              >
              Have I done something wrong?
              >
              Here's a complete project that should work (note it will fire when you
              click the [+] or [-] buttons giving the wrong node text). Just copy
              this into a new form in your project and press F5 to run it.
              >
              Public Class Form1
              >
              Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
              System.EventArg s) Handles MyBase.Load
              Dim treeView As New TreeView()
              Dim nodeA As New TreeNode("NodeA ")
              Dim nodeB As New TreeNode("NodeB ")
              For i As Integer = 1 To 5
              nodeA.Nodes.Add (String.Format( "NodeA child {0}", i))
              nodeB.Nodes.Add (String.Format( "NodeB child {0}", i))
              Next
              treeView.Nodes. Add(nodeA)
              treeView.Nodes. Add(nodeB)
              >
              treeView.Dock = DockStyle.Fill
              treeView.Expand All()
              AddHandler treeView.NodeMo useClick, AddressOf
              treeview_NodeMo useClick
              Me.Controls.Add (treeView)
              >
              End Sub
              >
              Private Sub treeview_NodeMo useClick(ByVal sender As Object, ByVal
              e As TreeNodeMouseCl ickEventArgs)
              Dim node As TreeNode = DirectCast(send er,
              TreeView).Selec tedNode
              >
              If node.Parent Is Nothing Then
              MsgBox(String.F ormat("You clicked a parent node named
              {0}", node.Text))
              Else
              Dim parent As TreeNode = node.Parent
              While Not parent.Parent Is Nothing
              parent = parent.Parent
              End While
              MsgBox(String.F ormat("The node's parent is {0}",
              parent.Text))
              End If
              End Sub
              >
              End Class
              >
              Hope that helps!
              >
              Thanks,
              >
              Seth Rowe
              Thanks Seth (and everyone else) - works a treat

              Comment

              Working...