Treeview duplicating nodes
-
Hi all. I've got a problem. I'm developing an app on VB.NET, and using a treeview control. I need to check which of all the nodes on the treeview are marked with the checkbox, and it impossible to know how many of them there are, or how deep the tree goes on the nodes level, so I have developed a recursive function, that receives the node as a parameter. It works fine, and makes what I need. The problem is that every time I make this operation, the nodes seem to duplicate in the treeview, although it is only visually, because when I check treeview.nodes.count property, it says there are only the number of nodes I had at the beggining. I have developed a simple app with just a treeview and a button to make this operation, and it still seems to happen so I'm becoming quite desperate. Does somebody know why does it happens?? If someone needs to see the example, I can send it by email. Thanks.
Time to come clean... Vive y deja vivir / Live and let live Javier
-
Hi all. I've got a problem. I'm developing an app on VB.NET, and using a treeview control. I need to check which of all the nodes on the treeview are marked with the checkbox, and it impossible to know how many of them there are, or how deep the tree goes on the nodes level, so I have developed a recursive function, that receives the node as a parameter. It works fine, and makes what I need. The problem is that every time I make this operation, the nodes seem to duplicate in the treeview, although it is only visually, because when I check treeview.nodes.count property, it says there are only the number of nodes I had at the beggining. I have developed a simple app with just a treeview and a button to make this operation, and it still seems to happen so I'm becoming quite desperate. Does somebody know why does it happens?? If someone needs to see the example, I can send it by email. Thanks.
Time to come clean... Vive y deja vivir / Live and let live Javier
Just guessing that it's something in the recursive function. Go ahead and post your code so we can see it. If needed, make a generic example with your code to reproduce the problem and to be concise. It's better to post the code here rather than send email so that others can possibly learn.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Just guessing that it's something in the recursive function. Go ahead and post your code so we can see it. If needed, make a generic example with your code to reproduce the problem and to be concise. It's better to post the code here rather than send email so that others can possibly learn.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
I'm sorry!! Here is the code. I just created a Windows VB.NET application, and put on it a treeview named TreeView1 and a button named Button1. Also, insert some random nodes on the treeview, doesn't matter, because all will appear duplicated. The recursive function is Recorre, as you can see, this function will read all the nodes on the treeview, but won't write anything on it, so, it is so weird for me when I get all the data duplicated. Imports System.Windows.Forms Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TreeView1.ExpandAll() End Sub Private Sub Recorre(ByRef Nodo As TreeNode) Dim i As Integer For i = 0 To Nodo.Nodes.Count - 1 Recorre(Nodo.Nodes(i)) Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Dim TreeView2 As TreeView TreeView1.BeginUpdate() For i = 0 To TreeView1.Nodes.Count - 1 Recorre(TreeView1.Nodes(i)) Next TreeView2 = New TreeView TreeView2 = TreeView1 Me.Controls.Remove(TreeView1) TreeView1 = Nothing TreeView1 = New TreeView Me.Controls.Add(TreeView1) TreeView1.Width = 176 TreeView1.Height = 242 TreeView1.Top = 12 TreeView1.Left = 12 TreeView1.CheckBoxes = True TreeView1.Refresh() TreeView1.EndUpdate() TreeView1 = TreeView2 End Sub End Class
Time to come clean... Vive y deja vivir / Live and let live Javier
-
I'm sorry!! Here is the code. I just created a Windows VB.NET application, and put on it a treeview named TreeView1 and a button named Button1. Also, insert some random nodes on the treeview, doesn't matter, because all will appear duplicated. The recursive function is Recorre, as you can see, this function will read all the nodes on the treeview, but won't write anything on it, so, it is so weird for me when I get all the data duplicated. Imports System.Windows.Forms Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TreeView1.ExpandAll() End Sub Private Sub Recorre(ByRef Nodo As TreeNode) Dim i As Integer For i = 0 To Nodo.Nodes.Count - 1 Recorre(Nodo.Nodes(i)) Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Dim TreeView2 As TreeView TreeView1.BeginUpdate() For i = 0 To TreeView1.Nodes.Count - 1 Recorre(TreeView1.Nodes(i)) Next TreeView2 = New TreeView TreeView2 = TreeView1 Me.Controls.Remove(TreeView1) TreeView1 = Nothing TreeView1 = New TreeView Me.Controls.Add(TreeView1) TreeView1.Width = 176 TreeView1.Height = 242 TreeView1.Top = 12 TreeView1.Left = 12 TreeView1.CheckBoxes = True TreeView1.Refresh() TreeView1.EndUpdate() TreeView1 = TreeView2 End Sub End Class
Time to come clean... Vive y deja vivir / Live and let live Javier
I don't get a duplication of nodes, I don't get any nodes back on treeview1. It would appear that your temp. clearing the nodes in treeview1 and then readding them? Having said that, I would do several things different. Instead of completely getting rid of treeview1 and readding it to the form and setting its properties, just clear the nodes (treeview1.nodes.clear). Probably don't need to create a whole another treeview to store the nodes (seems overkill), could use a treenodecollection instead. Again, I'm not sure what this is supposed to do other than restore the nodes?
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
I don't get a duplication of nodes, I don't get any nodes back on treeview1. It would appear that your temp. clearing the nodes in treeview1 and then readding them? Having said that, I would do several things different. Instead of completely getting rid of treeview1 and readding it to the form and setting its properties, just clear the nodes (treeview1.nodes.clear). Probably don't need to create a whole another treeview to store the nodes (seems overkill), could use a treenodecollection instead. Again, I'm not sure what this is supposed to do other than restore the nodes?
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
It seems to be a problem when sending the node byref, using byval I don't get the error... O_o So, the code could be just like this: Imports System.Windows.Forms Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TreeView1.ExpandAll() End Sub Private Sub Recorre(ByVal Nodo As TreeNode) Dim i As Integer For i = 0 To Nodo.Nodes.Count - 1 Recorre(Nodo.Nodes(i)) Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer For i = 0 To TreeView1.Nodes.Count - 1 Recorre(TreeView1.Nodes(i)) Next End Sub End Class
Time to come clean... Vive y deja vivir / Live and let live Javier