This code demonstrates a problem using a treeview twice

F

Franky

Been having a problem using a treeview. Work great the first time the form
containing it is entered but not the second time.

Took a while to create a small sample that exhibits the problem, but here it
is.

Seems to have something to do with doing ShowDialog

Easy to reproduce this project, simply add two forms.

One with a Button and one with a TreeView

The one with a Button, Form2, is the Start Form

The WriteLine statements make it easy to see what is happening

Public Class Form2
'This is the Start Form
Private mForm1 As Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim zz As DialogResult = mForm1.ShowDialog()
End Sub
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
mForm1 = New Form1
End Sub
End Class

The Form contains a TreeView
Imports System.IO
Imports VB = Microsoft.VisualBasic
Public Class Form1
Public Sub ExpandPath(ByVal pathToFind As String)
If Directory.Exists(pathToFind) Then 'If directory does not exists, don't
look
Console.WriteLine("SEARCH IN TOPMOST COLLECTION")
SearchHere(TreeView1.Nodes, pathToFind.ToUpper)
End If
End Sub
Private Sub SearchHere(ByVal currentTreeCollection As TreeNodeCollection,
ByVal pathToFind As String)
Dim tnPath As String
Static zz As Integer
zz += 1
Dim z As Integer = zz
For Each tn As TreeNode In currentTreeCollection
tnPath = tn.Tag.ToUpper
If tnPath.Length <= pathToFind.Length Then
If pathToFind = tnPath Then
Dim tp As TreeNode = tn.Nodes.Add("DUMMY")
tp.Tag = "DUMMY"
tn.Expand()
Console.WriteLine(z.ToString & "DONE " & tn.Tag)
TreeView1.SelectedNode = tn
Exit For
ElseIf pathToFind.Substring(0, tnPath.Length) = tnPath Then
Dim tp As TreeNode = tn.Nodes.Add("DUMMY")
tp.Tag = "DUMMY"
tn.Expand()
Console.WriteLine(z.ToString & "NOW SEARCH IN " & tn.Tag)
SearchHere(tn.Nodes, pathToFind)
Exit For
End If
End If
Next
Console.WriteLine(z.ToString & "SEARCH EXITED ")
End Sub
Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As
System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
'Clears out any nodes (especially the dummy) and then adds folder nodes
Dim SubNode As TreeNode
Console.WriteLine("Entered BeforeExpand " & e.Node.Tag)
e.Node.Nodes.Clear()
For Each subDir As String In Directory.GetDirectories(e.Node.Tag & "\") 'Now
add the folder's contents to the tree
SubNode = e.Node.Nodes.Add(IO.Path.GetFileName(subDir))
SubNode.Tag = subDir
If Directory.GetDirectories(subDir).Length <> 0 _
OrElse ((Directory.GetFiles(subDir).Length > 0)) Then
'If not empty add a dummy node
Dim tn As TreeNode = SubNode.Nodes.Add("DUMMY")
tn.Tag = "DUMMY"
End If
Next subDir
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
Static beenHereBefore As Boolean 'Need to initialize the tree
If Not beenHereBefore Then 'Add C: to the tree the first time
Dim SubNode As TreeNode = TreeView1.Nodes.Add("C:")
SubNode.Tag = "C:"
Dim tn As TreeNode = SubNode.Nodes.Add("DUMMY")
tn.Tag = "DUMMY"
End If
ExpandPath("C:\Documents and Settings\All Users\Start Menu")
End Sub
End Class
 
T

Tom Shelton

Been having a problem using a treeview. Work great the first time the form
containing it is entered but not the second time.

Took a while to create a small sample that exhibits the problem, but here it
is.

Seems to have something to do with doing ShowDialog

Franky - the problem has nothing to do with ShowDialog. The problem is that
you are keeping a reference to the form and showing it over and over again -
so it never gets it's state reset.

You can fix it one of two ways...

1. change the form load of the treeview form like this:


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Static beenHereBefore As Boolean 'Need to initialize the tree
'If Not beenHereBefore Then 'Add C: to the tree the first time
TreeView1.Nodes.Clear()
Dim SubNode As TreeNode = TreeView1.Nodes.Add("C:")
SubNode.Tag = "C:"
Dim tn As TreeNode = SubNode.Nodes.Add("DUMMY")
tn.Tag = "DUMMY"
'End If
ExpandPath("C:\Program Files")
End Sub


Notice, I'm just letting the tree re-init each time.

2. Change the calling form to not keep a reference at all - and then just
create a new form each time the button is clicked:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Using f As New Form1
f.ShowDialog(Me)
End Using
End Sub


HTH
 
F

Franky

I know if I reinitialize the treeview it woks OK.
But I wanted to retain the reference so that it would retain the users last
expansions.
When I show it the second time I want it to look like it did when it
finished the last time.
Not possible?
If it stays in memory why does it behave differently if I leave and
comeback?

If I do the search twice with the first ShowDialog, it works OK both times.

Thanks for the reply
 
F

Franky

I know if I reinitialize the treeview it woks OK.
But I wanted to retain the reference so that it would retain the users
last expansions.
When I show it the second time I want it to look like it did when it
finished the last time.
Not possible?
If it stays in memory why does it behave differently if I leave and
comeback?

If I do the search twice with the first ShowDialog, it works OK both
times.

Do this in Load and notice that there are two folders expanded.

ExpandPath("C:\Documents and Settings\All Users\Start Menu")

ExpandPath("C:\Documents and Settings\All Users\Documents\My Music")

However, the equivalent can't be done by putting and clicking a second
button on Form2 to expand My Music without collapsing StartMenu
 

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