Solved

Adding to the Treeview

Posted on 2011-03-19
6
917 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
[X]
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
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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