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
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