Treeview problem adding nodes

cadwalli

Active member
Joined
May 27, 2010
Messages
41
Location
United Kingdom
Programming Experience
10+
Hi

Is anyone able to help please?

I have a treeview class which loads all my treeview data. However, the only way I can get to display the treeview once ive built it in the class to to copy all the nodes into the treeview on the form already.

For example, the form has treeview1 on it, its empty because nothings been loaded yet. My code build the treeview from data in the DB and then returns that data formatted as type "treeview".

But as the treeview i want to update is already on the screen i have to use the "clone" method and rebuild teh treeview nodes from the ones returned in the code.

Is there not a simpler way to do this - i.e. treeview1 = treeview2?

Hope that makes sense.

Ian
 
Create and return the nodes:
Private Function CreateNodes() As TreeNode()
    Dim nodes As New List(Of TreeNode)
    Dim node As New TreeNode("root1")
    nodes.Add(node)
    node.Nodes.Add("child1")
    Return nodes.ToArray
End Function

Add them to the control with AddRange method:
Me.TreeView1.Nodes.AddRange(CreateNodes)
 
Hi

Im getting and error on the "Return nodes.ToArray" statement:

Value of type 1-dimensional array of system.windows.forms.treenode cannot be converted to system.windows.forms.treenode

i cant see any obvious issue, and im a little confused as it seems to be trying to convert treenode to treenode and has a problem?

Ian
 
Back
Top