Question Node displayed multiple times in treeview control

ubuntu118

New member
Joined
Apr 2, 2009
Messages
2
Programming Experience
10+
Hello all
I'm developing a windows application. I have a form in which there is a list box and a treeview control. My purpose is to fill treeview according to list item clicks. This is done using a pair of subroutines called ReloadPermissions and FillMenuItem:

VB.NET:
	Private Sub ReloadPermissions()
		Dim p As New Permissions
		Dim i As Integer
		Dim it As TreeNode
		Dim el As New List(Of String)

		p.LoadEntire(Main.DBC, 0)
		GetExpandedItems(tvPermissions.Nodes, el)
		tvPermissions.Nodes.Clear()
		For i = 0 To p.Count - 1
			it = New TreeNode
			it.Text = p(i).UrlText
			it.Tag = p(i).ID
			If p(i).Hidden Then it.ForeColor = Color.Red
			tvPermissions.Nodes.Add(it)
			FillMenuItem(it, p(i))
		Next
		el.Sort()
		ExpandItems(tvPermissions.Nodes, el)
	End Sub

	Private Sub FillMenuItem(ByRef tn As TreeNode, ByVal P As Permission)
		Dim i As Integer
		Dim it As TreeNode

		For i = 0 To P.Children.Count - 1
			it = New TreeNode
			it.Text = P.Children(i).UrlText
			it.Tag = P.Children(i).ID
			If P.Children(i).Hidden Then it.ForeColor = Color.Red
			tn.Nodes.Add(it)
			FillMenuItem(it, P.Children(i))
		Next
	End Sub

When list is clicked, ReloadPermissions is called:

VB.NET:
	Private Sub lstTemplates_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstTemplates.Click
		If lstTemplates.SelectedIndex < 0 Then Exit Sub

		Dim s As String = lstTemplates.SelectedItem.ToString
		Dim tm As New PermissionTemplate(Main.DBC, CType(lstTemplates.SelectedItem, ListItem).Value)

		ReloadPermissions()
		CheckPermissions(tm.Permissions)
		txtTitle.Text = tm.Title
		rbEdit.Select()
	End Sub

There are only two subroutines in which nodes are added to treeview control, and they are subroutines above. The problem is that whenever list item is clicked, node count displayed in treeview is increased!!! I checked number of nodes before and after call to tvPermissions.Nodes.Clear() using MsgBox. Results show that number of nodes isn't increased (1 before call, 0 after call, again 1 after ReloadPermissions() is called), but what is displayed doesn't show such a behavior! Refreshing window by putting it under other windows or call to tvPermissions.Refresh() has no effect.
Can anybody help me?!
 
Found solution!

I found it myself!!!! In CheckPermissions() sub, I have called another sub named ApplyCheckedCount to which treeview nodes are passed by reference. I changed it to byval (cuz I have other subs and funcs to which tree nodes are passed by value and they don't cause this problem) and problem solved! Damn! When passed by reference, actually no node is added, but they are displayed duplicated!
 
Back
Top