TreeView Confusion: Top Node Duplicated !?!? (Updated Info)

E

eBob.com

(The original version of this, posted well over 12 hours ago has never shown
up. So I am reposting it with
some updated information.)

Just wrote my first TreeView code. Then decided to do something useful with
it so decided to use a TreeView to display a directory structure. Cheated a
bit and translated some C# code I found on the Internet. It seems to work
well EXCEPT that the topmost node is duplicated. That is, if I tell it to
start at d:\xyz there will be TWO d:\xyz nodes in the resulting tree. Both
at the top/highest level and both correct. But of course there shouldn't be
a second node.

I've put in beaucoup debugging code and the second node doesn't seem to be
coming from my code. The populating of the tree is initiated by a GO!
button. I've verified that this GO! button click routines is entered only
once. I also have a msgbox at the end of the recursively called population
routine and can see that the second node is being added after it's final
return. Which I think must mean that the second node is not being added by
my code.

(Update) I have also commented out all but the entry and exit debugging code
in PopulateTreeView and still get the duplicate node. If I comment out the
call to
PopulateTreeView then, finally, there is only one node. I also went back to
the
original C# code and created my very first C# application and it has no
problem.

A side issue, I tried to use Node.Expand so that I could watch the tree
being built but no nodes expand until I click on a plus sign.

So, here's the code if someone has sufficient sympathy and time to look at
it. I'd be very grateful. It's really not much code execpt for the debug
code. The designer is used only to create the form. ALL controls are
placed on the from dynamically.

Thanks, Bob

Public Class Form1

Dim tv As New TreeView
Dim tbxDebug As New TextBox
Dim depth As Integer = -1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim InDir As New TextBox
InDir.Location = New Point(10, 10)
Me.Controls.Add(InDir)

Dim btnGo As New Button
btnGo.Location = New Point(10, 40)
btnGo.Text = "GO!"
Me.Controls.Add(btnGo)
AddHandler btnGo.Click, AddressOf btnGo_Click

With tbxDebug
.Location = New Point(10, 70)
.Multiline = True
.ScrollBars = ScrollBars.Both
.Width = 800
.Height = 200
End With
Me.Controls.Add(tbxDebug)

With tv
.Location = New Point(10, 280)
.Scrollable = True
.Width = 500
.Height = 500
.CheckBoxes = True
End With
Me.Controls.Add(tv)

End Sub

Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs)

MsgBox("click entered")

'Dim path As String = "d:\Program files"
Dim path As String = "d:\icons"
Dim basenode As TreeNode = New TreeNode(path)
tv.Nodes.Add(basenode)
basenode.Expand()


PopulateTreeView(path, tv.Nodes(0))

End Sub

Sub PopulateTreeView(ByRef directoryValue As String, ByRef parentNode As
TreeNode)

tbxDebug.AppendText("called for directory: """ & directoryValue & _
""" / parentnode: " & parentNode.ToString &
vbCrLf)
depth += 1

Dim directoryArray() As String =
Directory.GetDirectories(directoryValue)

If (directoryArray.Length <> 0) Then

For Each directory As String In directoryArray
Dim substringDirectory As String
Try
substringDirectory = directory.Substring( _
directory.LastIndexOf("\") + 1, _
directory.Length - directory.LastIndexOf("\") - 1)

tbxDebug.AppendText("substringDirectory is: " &
substringDirectory & vbCrLf)

Dim myNode As TreeNode = New
TreeNode(substringDirectory)

parentNode.Nodes.Add(myNode)
myNode.Expand()
MsgBox("pace")

PopulateTreeView(directory, myNode)
Catch UnauthorizedAccessException As Exception
MsgBox("caught: " & directory)
parentNode.Nodes.Add("Access denied <" & directory & ">"
& vbCrLf)
End Try
Next
End If

tbxDebug.AppendText("returning from handling directory: " &
directoryValue & vbCrLf)
MsgBox("returning from handling directory: " & directoryValue)

End Sub ' PopulateTreeView

End Class
 
E

eBob.com

Captain Jack said:
What's biting you here is the ByRef calling mode for the variable
parentNode (you don't need it on the string variable either, but that one
isn't causing the problem). I' don't recall what's happening internally (I
think the compiler is creating a copy of the class pointer when you pass
it by reference, which is different behavior from other platforms), but if
you pass it ByVal, you won't have the problem.The only reason you should
need to pass it by reference would be if you were instantiating the class
in the function, which isn't the case here.
Thank you Captian. I had a vague suspicion that the problem might have
something to do with that but never took it seriously. I'm just plain
careless about how I pass variables and have never had a problem before. I
have to give it some thought and try to figure out how passing the TreeNode
by reference can have the result I see.

Thanks again, Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top