Results 1 to 7 of 7

Thread: [RESOLVED] tree refresh with a memory

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] tree refresh with a memory

    ok, so I have been at this awhile and I just can't seem to nail it down. The current code works fine on the very first fresh but after that it doesn't remember what node was clicked. For this refresh I am looking for a ini file and if found I delete the ini file and refresh the tree. The tree has only 2 parents and never anymore then that.

    This first part is the timer sub
    Code:
    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            
            Dim SelectNode As String = Me.tvFolders.SelectedNode.Name
            Try
                
                ' check to see if file exist, if so do refresh then delete file
                If (File.Exists(appPath & "refresh.ini")) Then
                    File.Delete(appPath & "refresh.ini")
                    Me.Update()
                    ' this clears icons, 
                    lvFiles.Items.Clear()
                    FillTreeView()
                    InitializeTimer()
    
                    'MsgBox("1 " & SelectNode)
    
                    'Me.tvFolders.SelectedNode = curNode
                    If curNode.ToString.Length > 0 Then
                        If objFS.SearchTree(SelectNode, tvFolders.Nodes("Parent1")) Then
                           
                            found = True
                            
                        ElseIf objFS.SearchTree(SelectNode, tvFolders.Nodes("Parent2")) Then
                           
                            found = True
                            
                        End If
    
                    Else
                        'MsgBox("curnode is blank")
                        Me.tvFolders.SelectedNode = tvFolders.Nodes("Jackson County")
                    End If
                        'MsgBox(Me.tvFolders.SelectedNode)
                        objFS.ListFoldersFiles(Me.tvFolders.SelectedNode.Tag, lvFiles, ImageList2)
                        'MsgBox(curNode.Text)
                        Timer2.Enabled = False
                        Cursor.Current = System.Windows.Forms.Cursors.Default
    
                        ToolStripStatusLabel1.Text = ""
                        Me.tvFolders.Focus()
    
                    Else
                        InitializeTimerRefresh()
                    End If
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
                MsgBox("Refresh Failed" & Chr(13) & Chr(13) & ex.Message, vbOKOnly, "Failed To Refresh Timer")
            End Try
        End Sub
    This next part is the search function.
    Code:
     Public Function SearchTree(ByVal sSearchString As String, ByVal startNode As TreeNode) As Boolean
            MsgBox("1 " & startNode.Name)
            MsgBox("2 " & sSearchString)
            If CBool(InStr(1, startNode.Name, sSearchString, vbTextCompare)) Then
                Form1.tvFolders.SelectedNode = startNode
                Return True
            
            End If
            For Each n As TreeNode In startNode.Nodes
                If SearchTree(sSearchString, n) Then
                    found = True
                    Exit For
                End If
            Next
            If found Then
                Return True
            Else
                MsgBox("Failed to find")
                Return False
            End If
            'Return True
            'lLastNodeFound = 0
        End Function
    Now the main problem is if you click on a child in parent2 and hit refresh it is not sending Parent2 as a treenode to the search function and it doesn't remember the node clicked. But if you click on a child in parent1 it remembers the node but always refreshes to the top parent1 node. but the very first time you refresh in parent1 it works perfect. I have a feeling my logic is messed up. Because even on the second refresh regardless of which parent you are in it never comes back with failed message from the search function.

    Can somebody please point out what I might be doing wrong?

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: tree refresh with a memory

    Anybody willing to give it a try?

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: tree refresh with a memory

    How do you populate the treeview? Can you show the code for the sub FillTreeView()?

    I'm thinking about a much easier approach which requires you to assign a unique name to each node when you populate the treeview. Then when a node is selected, you save the node.name and when you re-populate the treeview, you can search for a node with that name in a few lines of code
    Code:
        Dim theNode As TreeNode = TreeView1.Nodes.Find(savedNodeName, True)
        If theNode IsNot Nothing Then
             TreeView1.SelectedNode = theNode
        End If
    Last edited by stanav; Oct 6th, 2011 at 03:50 PM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: tree refresh with a memory

    Here is how I am doing it, I do give them a unique name. basically just loading a directory structure from a path on C:
    Code:
    Private Sub FillTreeView()
            Try
                For Each n As TreeNode In tvFolders.Nodes
                    n.Nodes.Clear()
                Next
                Me.PopulateTreeNodes(appPath, tvFolders.Nodes("Parent1"))
                'Populate root2 (favorites) on treeview 
    
                For x As Integer = My.Settings.Favorites.Count - 1 To 0 Step -1
    
                    If Not favorites(x) Is Nothing Then
                        AddFavoriteNode(favorites(x))
                    End If
                Next
                ToolStripStatusLabel1.Text = ""
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
                MsgBox("Failed to Load Tree" & Chr(13) & Chr(13) & ex.Message, vbOKOnly, "Failed To Load Tree")
            End Try
        End Sub
    
        Private Sub PopulateTreeNodes(ByVal dir As String, ByVal parentNode As TreeNode)
            Try
                Dim favorites As System.Collections.Specialized.StringCollection = My.Settings.Favorites
                Dim folders() As String = Directory.GetDirectories(dir)
                Dim node As TreeNode = Nothing
                Dim nodeText As String = String.Empty
                For Each folder As String In folders
                    nodeText = Path.GetFileName(folder)
                    node = New TreeNode(nodeText)
                    node.Tag = folder
                    node.Name = nodeText
                    If Not My.Settings.Favorites.Contains(nodeText) Then
                        If parentNode Is Nothing Then
                            tvFolders.Nodes.Add(node)
                            node.ImageIndex = 0
                            node.SelectedImageIndex = 1
                        Else
                            parentNode.Nodes.Add(node)
                            parentNode.ImageIndex = 2
                            parentNode.SelectedImageIndex = 2
    
                        End If
                        PopulateTreeNodes(folder, node)
                    End If
                Next
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
                MsgBox("Failed to Populate" & Chr(13) & Chr(13) & ex.Message, vbOKOnly, "Failed To Populate")
            End Try
        End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: tree refresh with a memory

    If I try to use your idea and do this

    Code:
    Dim SelectNode As String = Me.tvFolders.SelectedNode.Name
     Dim theNode As TreeNode = tvFolders.Nodes.Find(SelectNode, True)
                        If theNode IsNot Nothing Then
                            tvFolders.SelectedNode = theNode
                        End If
    I get an error on the bold line

    Value of type '1-dimensional array of System.Windows.Forms.TreeNode' cannot be converted to 'System.Windows.Forms.TreeNode'.

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: tree refresh with a memory

    I coded those lines from thin air and didn't remember correctly that the method returns a treenode array. Sorry for that.
    You can change the code to this then:
    Code:
    'You have to save the selected node name BEFORE you clear the treeview and re-populate it. 
    'Then after you repopulate the treeview, you do this
    Dim theNodes() as treenode = tvFolders.nodes.Find(selecteNode, true)
    If theNodes.Length > 0 Then
       tvFolders.SelectedNode = theNodes(0)
    End If
    By the way, I see that you use the node text as the node name also. That is not good since it will not guarantee the name is unique (you can have 2 different folders with the same name in 2 different location, can't you?) Therefore, you should use the folder'd full path as the name. Change this line
    Code:
    node.Name = nodeText
    To this:
    Code:
    node.Name = folder
    Last edited by stanav; Oct 6th, 2011 at 11:36 PM.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: tree refresh with a memory

    Thanks stanav, the find code works good. And good catch on the folder naming. I made both changes and all is working good.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width