This code demonstrates a problem using a treeview twice

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Franky

    #1

    This code demonstrates a problem using a treeview twice

    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


  • Tom Shelton

    #2
    Re: This code demonstrates a problem using a treeview twice

    On 2006-12-08, Franky <frankieNOSPAM@ a-znet.comwrote:
    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(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
    TreeView1.Nodes .Clear()
    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:\ 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(B yVal sender As Object, ByVal e As System.EventArg s) Handles Button1.Click
    Using f As New Form1
    f.ShowDialog(Me )
    End Using
    End Sub


    HTH

    --
    Tom Shelton

    Comment

    • Franky

      #3
      Re: This code demonstrates a problem using a treeview twice

      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



      "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
      news:2JOdnYpQTd wzr-fYnZ2dnUVZ_qunn Z2d@comcast.com ...
      On 2006-12-08, Franky <frankieNOSPAM@ a-znet.comwrote:
      >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(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
      TreeView1.Nodes .Clear()
      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:\ 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(B yVal sender As Object, ByVal e As
      System.EventArg s) Handles Button1.Click
      Using f As New Form1
      f.ShowDialog(Me )
      End Using
      End Sub
      >
      >
      HTH
      >
      --
      Tom Shelton

      Comment

      • Franky

        #4
        Re: This code demonstrates a problem using a treeview twice

        >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

        >
        Thanks for the reply
        >
        >
        >
        "Tom Shelton" <tom_shelton@co mcastXXXXXXX.ne twrote in message
        news:2JOdnYpQTd wzr-fYnZ2dnUVZ_qunn Z2d@comcast.com ...
        >On 2006-12-08, Franky <frankieNOSPAM@ a-znet.comwrote:
        >>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(ByVa l sender As Object, ByVal e As
        >System.EventAr gs) 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.A dd("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(B yVal sender As Object, ByVal e As
        >System.EventAr gs) Handles Button1.Click
        > Using f As New Form1
        > f.ShowDialog(Me )
        > End Using
        > End Sub
        >>
        >>
        >HTH
        >>
        >--
        >Tom Shelton
        >
        >


        Comment

        Working...