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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim zz As DialogResult = mForm1.ShowDial og()
End Sub
Private Sub Form2_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles Me.Load
mForm1 = New Form1
End Sub
End Class
The Form contains a TreeView
Imports System.IO
Imports VB = Microsoft.Visua lBasic
Public Class Form1
Public Sub ExpandPath(ByVa l pathToFind As String)
If Directory.Exist s(pathToFind) Then 'If directory does not exists, don't
look
Console.WriteLi ne("SEARCH IN TOPMOST COLLECTION")
SearchHere(Tree View1.Nodes, pathToFind.ToUp per)
End If
End Sub
Private Sub SearchHere(ByVa l currentTreeColl ection As TreeNodeCollect ion,
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 currentTreeColl ection
tnPath = tn.Tag.ToUpper
If tnPath.Length <= pathToFind.Leng th Then
If pathToFind = tnPath Then
Dim tp As TreeNode = tn.Nodes.Add("D UMMY")
tp.Tag = "DUMMY"
tn.Expand()
Console.WriteLi ne(z.ToString & "DONE " & tn.Tag)
TreeView1.Selec tedNode = tn
Exit For
ElseIf pathToFind.Subs tring(0, tnPath.Length) = tnPath Then
Dim tp As TreeNode = tn.Nodes.Add("D UMMY")
tp.Tag = "DUMMY"
tn.Expand()
Console.WriteLi ne(z.ToString & "NOW SEARCH IN " & tn.Tag)
SearchHere(tn.N odes, pathToFind)
Exit For
End If
End If
Next
Console.WriteLi ne(z.ToString & "SEARCH EXITED ")
End Sub
Private Sub TreeView1_Befor eExpand(ByVal sender As Object, ByVal e As
System.Windows. Forms.TreeViewC ancelEventArgs) Handles TreeView1.Befor eExpand
'Clears out any nodes (especially the dummy) and then adds folder nodes
Dim SubNode As TreeNode
Console.WriteLi ne("Entered BeforeExpand " & e.Node.Tag)
e.Node.Nodes.Cl ear()
For Each subDir As String In Directory.GetDi rectories(e.Nod e.Tag & "\") 'Now
add the folder's contents to the tree
SubNode = e.Node.Nodes.Ad d(IO.Path.GetFi leName(subDir))
SubNode.Tag = subDir
If Directory.GetDi rectories(subDi r).Length <0 _
OrElse ((Directory.Get Files(subDir).L ength 0)) Then
'If not empty add a dummy node
Dim tn As TreeNode = SubNode.Nodes.A dd("DUMMY")
tn.Tag = "DUMMY"
End If
Next subDir
End Sub
Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
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.A dd("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(B yVal sender As Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim zz As DialogResult = mForm1.ShowDial og()
End Sub
Private Sub Form2_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles Me.Load
mForm1 = New Form1
End Sub
End Class
The Form contains a TreeView
Imports System.IO
Imports VB = Microsoft.Visua lBasic
Public Class Form1
Public Sub ExpandPath(ByVa l pathToFind As String)
If Directory.Exist s(pathToFind) Then 'If directory does not exists, don't
look
Console.WriteLi ne("SEARCH IN TOPMOST COLLECTION")
SearchHere(Tree View1.Nodes, pathToFind.ToUp per)
End If
End Sub
Private Sub SearchHere(ByVa l currentTreeColl ection As TreeNodeCollect ion,
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 currentTreeColl ection
tnPath = tn.Tag.ToUpper
If tnPath.Length <= pathToFind.Leng th Then
If pathToFind = tnPath Then
Dim tp As TreeNode = tn.Nodes.Add("D UMMY")
tp.Tag = "DUMMY"
tn.Expand()
Console.WriteLi ne(z.ToString & "DONE " & tn.Tag)
TreeView1.Selec tedNode = tn
Exit For
ElseIf pathToFind.Subs tring(0, tnPath.Length) = tnPath Then
Dim tp As TreeNode = tn.Nodes.Add("D UMMY")
tp.Tag = "DUMMY"
tn.Expand()
Console.WriteLi ne(z.ToString & "NOW SEARCH IN " & tn.Tag)
SearchHere(tn.N odes, pathToFind)
Exit For
End If
End If
Next
Console.WriteLi ne(z.ToString & "SEARCH EXITED ")
End Sub
Private Sub TreeView1_Befor eExpand(ByVal sender As Object, ByVal e As
System.Windows. Forms.TreeViewC ancelEventArgs) Handles TreeView1.Befor eExpand
'Clears out any nodes (especially the dummy) and then adds folder nodes
Dim SubNode As TreeNode
Console.WriteLi ne("Entered BeforeExpand " & e.Node.Tag)
e.Node.Nodes.Cl ear()
For Each subDir As String In Directory.GetDi rectories(e.Nod e.Tag & "\") 'Now
add the folder's contents to the tree
SubNode = e.Node.Nodes.Ad d(IO.Path.GetFi leName(subDir))
SubNode.Tag = subDir
If Directory.GetDi rectories(subDi r).Length <0 _
OrElse ((Directory.Get Files(subDir).L ength 0)) Then
'If not empty add a dummy node
Dim tn As TreeNode = SubNode.Nodes.A dd("DUMMY")
tn.Tag = "DUMMY"
End If
Next subDir
End Sub
Private Sub Form1_Load(ByVa l sender As Object, ByVal e As System.EventArg s)
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.A dd("DUMMY")
tn.Tag = "DUMMY"
End If
ExpandPath("C:\ Documents and Settings\All Users\Start Menu")
End Sub
End Class
Comment