Solved

Adding to the Treeview

Posted on 2011-03-19
6
914 Views
Last Modified: 2012-08-14
I'm writing a vb.net application that will use the treeview control to display info to the users.  It works to add a node to the first level nodes, but when trying to add a node to the second level nodes, it adds a node but it has no information assigned to it.  The way I add a node to the tree is I create a tree node, then assign information to it, then add it to the tree.  I've displayed a message box with information about the treenode and all information is there before its added.  My code basically works this way below:

Get info for node0

Treeview.nodes.add(node0)

Get info for node1

Treeview.nodes(node0).nodes.add(node1)

Get info for node2

Treeview.nodes(node0).nodes(node1).nodes.add(node2)

Node2 doesn't get added correctly. All information is correct in Node2 and something does get added to the tree but with no information.  Has anyone had this problem before?  Thanks for any help.
0
Comment
Question by:rcblevins
  • 2
  • 2
  • 2
6 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 35171943
have you tried to keep a reference to the node you just added:

mynode0 = Treeview.nodes.add(node0)
mynode1 = mynode0.nodes.add(node1)
mynode1.nodes.add(node2)
0
 
LVL 12

Expert Comment

by:omegaomega
ID: 35172307
Hello, rcblevins,

I'm not sure what you mean by "information", but as far as I know Nodes requires an integer index, so an expression like:

    Treeview.nodes(node0).nodes.add(node1)

should cause an error.  If you can show the actual code that you are using to populate the TreeView we may be able to provide more help.

Cheers,
Randy


0
 

Author Comment

by:rcblevins
ID: 35172373
Here is the code.  The problem is where node2 is being used.  The stn node has information in it when its added to the tree, but the tree shows that a blank node is added.  The other wierd thing is that I also tried to show the name of the node refereced by node1 which should be node2's parent and it's name comes back empty.  Thanks for your help.

Dim node0 As Integer
        Dim node1 As Integer
        Dim node2 As Integer
        Dim node3 As Integer
        Dim node4 As Integer


        TreeView.Nodes.Clear()

        TreeView.BeginUpdate()

              Dim mddr As SqlDataReader

            mddr = Get Data

            If (mddr.HasRows) Then

                While (mddr.Read())


                    Dim mdtn As New TreeNode


                    mdtn.Tag = mddr.GetInt32(0)
                    mdtn.Text = mddr.GetString(1)
                    mdtn.ToolTipText = mddr.GetInt32(0).ToString

                    node0 = TreeView.Nodes.Add(mdtn)

                    Dim cdr = Get Data

                    If (cdr.HasRows) Then

                        While cdr.Read()

                            Dim ctn As New TreeNode

                            ctn.Tag = cdr.GetInt32(0)
                            ctn.Text = cdr.GetString(1)
                            ctn.ToolTipText = cdr.GetInt32(0).ToString


                            node1 = TreeView.Nodes(node0).Nodes.Add(ctn)


                                Dim sdr = Get Data

                                If (sdr.HasRows) Then

                                    While sdr.Read()

                                        Dim stn As New TreeNode

                                        stn.Tag = sdr.GetString(0)
                                        stn.Name = sdr.GetString(1)


                                        node2 = TreeView.Nodes(node0).Nodes(node1).Nodes.Add(stn)

                                   End While

                                End If

                                sdr.Close()

                            End If

                        End While

                    End If

                    cdr.Close()

                End While


            End If

            mddr.Close()


        End If

        TreeView.EndUpdate()
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 35172452
>>node1 = TreeView.Nodes(node0).Nodes.Add(ctn)

node1 cannot be an integer. you have mixed your variables. see how I fill a treeview at http://www.emoreau.com/Entries/Articles/2006/04/The-Treeview-control.aspx
0
 
LVL 12

Accepted Solution

by:
omegaomega earned 500 total points
ID: 35172508
Hello, rcblevins,

I think that you just are missing a line that reads:

    stn.Text = ...

Cheers,
Randy
0
 

Author Comment

by:rcblevins
ID: 35172662
Lol!  I had looked and looked over that code.  I was thinking in the back of my mind that is was something simple.  I just needed someone else to see it I guess.  Thanks for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now