Programitically Adding items to TreeView

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QnJhZA==?=

    #1

    Programitically Adding items to TreeView

    VS,VB 2005

    I have a treeview which i load data into in the forms load event
    Adding Nodes and Children to the parent nodes works fine

    to add a parent:
    Me.tvGroups.Nod es.Add(GroupDat aRow(0)("MenuGr oupID").ToStrin g,
    GroupDataRow(0) ("GroupLabel "))

    to add a child to a Parent:
    ParentIndex =
    Me.tvGroups.Nod es.IndexOfKey(G roupDataRow(0)( "ParentGroupID" ).ToString)
    NodeRef = Me.tvGroups.Nod es(ParentIndex)
    NodeRef.Nodes.A dd(GroupDataRow (0)("MenuGroupI D").ToString ,
    GroupDataRow(0) ("GroupLabel "))

    all works good... but now im trying to add a child to a child and i am
    unable to get the parent index by key the same way as above. this code fails
    when its a child key im looking for:

    ParentIndex =
    Me.tvGroups.Nod es.IndexOfKey(G roupDataRow(0)( "ParentGroupID" ).ToString)

    suggestions???
  • =?Utf-8?B?QnJhZA==?=

    #2
    RE: Programitically Adding items to TreeView


    also, if i insert a new node, child, grandchild, etc... how do i make the
    inserted node the selected node?

    Comment

    • G Himangi

      #3
      Re: Programitically Adding items to TreeView

      To select the node, set its Selected property (from .Net 2.0 onwards) to
      true.

      ---------
      - G Himangi, Sky Software http://www.ssware.com
      Shell MegaPack : GUI Controls For Drop-In Windows Explorer like Shell
      Browsing Functionality For Your App (.Net & ActiveX Editions).
      EZNamespaceExte nsions.Net : Develop namespace extensions rapidly in .Net
      EZShellExtensio ns.Net : Develop all shell extensions,expl orer bars and BHOs
      rapidly in .Net
      ---------



      "Brad" <Brad@discussio ns.microsoft.co mwrote in message
      news:7ABB8994-2C25-4CDD-A810-D5F930513E5A@mi crosoft.com...
      >
      also, if i insert a new node, child, grandchild, etc... how do i make the
      inserted node the selected node?

      Comment

      • =?Utf-8?B?QnJhZA==?=

        #4
        Re: Programitically Adding items to TreeView

        useing .net 2

        first, i can find no Selected Property, what is it under?

        second, back to my first problem...

        if i am looking for the index to a node that is a granchild, using
        IndexOfKey returns a -1, it cant find the granchild. How do i find the Index?

        if i have an Index, how do i set the node to selected?




        "G Himangi" wrote:
        To select the node, set its Selected property (from .Net 2.0 onwards) to
        true.

        Comment

        • =?Utf-8?B?QnJhZA==?=

          #5
          Re: Programitically Adding items to TreeView

          solving my first problem gives me the solution for the rest of them

          this code is not my origional creation....

          i created 2 functions that when passed a Key Value (Nodes.Name) it will
          return the Node i referenced and the rest is simple

          this allows me to find a node that is a parent, child, grandchild, great
          grandchild, etc... by passing the key (or Name) associated with it...

          Public Function FindNodeByNodeN ame(ByVal NodeName As String) As TreeNode
          Return SelectNode(Me.t vGroups.Nodes, NodeName)
          End Function

          Private Function SelectNode(ByVa l Nodes As TreeNodeCollect ion, ByVal
          NodeName As String) As TreeNode
          Dim foundNode As TreeNode = Nothing
          Dim i As Integer = 0

          Do Until Not foundNode Is Nothing OrElse i = Nodes.Count
          If DirectCast(Node s(i), TreeNode).Name = NodeName Then
          foundNode = DirectCast(Node s(i), TreeNode)
          Else
          If Nodes(i).Nodes. Count 0 Then
          foundNode = SelectNode(Node s(i).Nodes, NodeName)
          End If

          i += 1
          End If
          Loop
          Return foundNode
          End Function

          Comment

          • =?Utf-8?B?QnJhZA==?=

            #6
            Re: Programitically Adding items to TreeView

            ..Selected???

            this works...

            Me.tvName.Selec tedNode = Me.tvName.Nodes (NodeIndexToSel ect)

            Comment

            • =?Utf-8?B?QnJhZA==?=

              #7
              Re: Programitically Adding items to TreeView

              while were at it, to locate a child, grandchild, etc.. to add another node
              to...

              could be spead up by useing tvname.nodes.fi nd(KeyText,True ) to see if you
              should even bother going down the tree any further

              Comment

              Working...