Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Adding to the Treeview

Posted on 2011-03-19
6
916 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 70

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 70

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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