|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
This code demonstrates a problem using a treeview twicecontaining 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 On 2006-12-08, Franky <frankieNOSPAM@a-znet.com> wrote:
> Been having a problem using a treeview. Work great the first time the form Franky - the problem has nothing to do with ShowDialog. The problem is that> 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 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 -- Tom Shelton 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 Show quoteHide quote "Tom Shelton" <tom_shel***@comcastXXXXXXX.net> wrote in message news:2JOdnYpQTdwzr-fYnZ2dnUVZ_qunnZ2d@comcast.com... > On 2006-12-08, Franky <frankieNOSPAM@a-znet.com> wrote: >> 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 > > -- > Tom Shelton >I know if I reinitialize the treeview it woks OK. Do this in Load and notice that there are two folders expanded.> 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. 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 Show quoteHide quote > > Thanks for the reply > > > > "Tom Shelton" <tom_shel***@comcastXXXXXXX.net> wrote in message > news:2JOdnYpQTdwzr-fYnZ2dnUVZ_qunnZ2d@comcast.com... >> On 2006-12-08, Franky <frankieNOSPAM@a-znet.com> wrote: >>> 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 >> >> -- >> Tom Shelton > >
destroying/releasing objects from memory?
Threading Delegate Question Interlocked.Add Not Thread Safe with Longs on a 32bit system Data Relations with DataTable Class vbs to vb.net Error: The transport failed to connect to the server How to Insert field and value into another grid how 2 approximate 0.1 to be 1 Looking for suggestions on how to do something slow form backimage load @ vb.net |
|||||||||||||||||||||||