7 Replies - 445 Views - Last Post: 18 January 2013 - 05:36 PM Rate Topic: -----

#1 The Gothic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-June 09

What is better for reading

Posted 18 January 2013 - 01:32 PM

Hi all,
first I am a newbie so take note please. I have two treeviews which are filled with .xls files. I am comparing them for any changes but if there are many nodes the application is not responding. Here is my code for better imagination:

'This colors parent nodes in 1st treeview which are in 2nd treeview green and those which are not red 
    Public Sub ColorParent()

        For Each Me.parentnode In TreeView1.Nodes

            FindParent = TreeView2.Nodes.Find(TreeView1.Nodes(parentnode.Index).Name, False)

            On Error Resume Next
            If FindParent.Count = 0 Then

                TreeView1.Nodes(parentnode.Index).BackColor = Color.Red
            Else
                TreeView1.Nodes(FindParent(0).Name).BackColor = Color.Green

            End If

        Next parentnode

    End Sub

'This colors all childs in 1st treeview with childs which are in 2nd treeview at same parents

    Public Sub ColorSameChilds()

        For Each Me.parentnode In TreeView1.Nodes

            For Each Me.parentnode2 In TreeView2.Nodes

                For Each Me.childnodeTV1 In TreeView1.Nodes(parentnode.Index).Nodes

                    For Each Me.childnodeTV2 In TreeView2.Nodes(parentnode2.Index).Nodes

                        FindChild = TreeView2.Nodes(parentnode2.Index).Nodes.Find(TreeView1.Nodes(parentnode.Index).Nodes(childnodeTV1.Index).Name, False)

                        On Error Resume Next
                        If FindChild.Count > 0 Then

                            If TreeView1.Nodes(parentnode.Index).BackColor = Color.Green Then

                                TreeView1.Nodes(parentnode.Index).Nodes(childnodeTV1.Index).BackColor = Color.Green

                            End If
                        End If

                    Next childnodeTV2

                Next childnodeTV1

            Next parentnode2

        Next parentnode

    End Sub

'This just color parents in 1st treeview to orange if there is any uncolored child
    Public Sub ColorDifferentChilds()

        For Each Me.parentnode In TreeView1.Nodes

            For Each Me.childnodeTV1 In TreeView1.Nodes(parentnode.Index).Nodes

                If TreeView1.Nodes(parentnode.Index).BackColor <> Color.Red Then

                    If TreeView1.Nodes(parentnode.Index).Nodes(childnodeTV1.Index).BackColor = Nothing Then

                        TreeView1.Nodes(parentnode.Index).Nodes(childnodeTV1.Index).BackColor = Color.Orange
                        TreeView1.Nodes(parentnode.Index).BackColor = Color.Orange

                    End If
                End If
            Next childnodeTV1

        Next parentnode

    End Sub



The problem is in ColorSameChilds() and I want to ask if there is some faster way how to read data. Can you tell me for example if reading dataset or array is faster then reading treeview. Or can you recommend me any article with sort of these information (useful tips to make fast code). There are about 1k parent nodes in treeview

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: What is better for reading

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3730
  • View blog
  • Posts: 13,025
  • Joined: 12-December 12

Re: What is better for reading

Posted 18 January 2013 - 02:01 PM

My first impression is that you don't need all those nested For..Each blocks because, at the end, you are only using

For Each Me.childnodeTV2 In


You can then (I'm assuming..) look back through these child-elements parentage to change colours.

But, as I say, I've only had a brief look.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3730
  • View blog
  • Posts: 13,025
  • Joined: 12-December 12

Re: What is better for reading

Posted 18 January 2013 - 02:10 PM

If you want to loop through ALL nodes then the following recursive method is probably more efficient:

Private Sub PrintRecursive(ByVal n As TreeNode)
   System.Diagnostics.Debug.WriteLine(n.Text)
   MessageBox.Show(n.Text)
   Dim aNode As TreeNode
   For Each aNode In n.Nodes
      PrintRecursive(aNode)
   Next
End Sub

' Call the procedure using the top nodes of the treeview.
Private Sub CallRecursive(ByVal aTreeView As TreeView)
   Dim n As TreeNode
   For Each n In aTreeView.Nodes
      PrintRecursive(n)
   Next
End Sub

Was This Post Helpful? 0
  • +
  • -

#4 The Gothic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-June 09

Re: What is better for reading

Posted 18 January 2013 - 02:37 PM

View Postandrewsw, on 18 January 2013 - 02:10 PM, said:

If you want to loop through ALL nodes then the following recursive method is probably more efficient:

Private Sub PrintRecursive(ByVal n As TreeNode)
   System.Diagnostics.Debug.WriteLine(n.Text)
   MessageBox.Show(n.Text)
   Dim aNode As TreeNode
   For Each aNode In n.Nodes
      PrintRecursive(aNode)
   Next
End Sub

' Call the procedure using the top nodes of the treeview.
Private Sub CallRecursive(ByVal aTreeView As TreeView)
   Dim n As TreeNode
   For Each n In aTreeView.Nodes
      PrintRecursive(n)
   Next
End Sub


I had to use all those nested blocks because when i had all in one it re-colored nodes to wrong color so i had to do it step by step. Can you please describe me your code a bit? I am new to VB and need to learn more tricks. I am adding a pic of correct output of my app:

How to add an image?

You can see that nodes which are same in LV1 and LV2 are green. If there are any new nodes they are red, same nodes with same childs are green and same parents with new childs are orange. For example in LV1 parent node 452677-B21 is orange because it has one new child against same parent node in LV2.

If you still do not understand me i can describe it more precisely

This post has been edited by The Gothic: 18 January 2013 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3730
  • View blog
  • Posts: 13,025
  • Joined: 12-December 12

Re: What is better for reading

Posted 18 January 2013 - 02:59 PM

I can't claim responsibility for that code: MSDN

Basically, the procedure CallRecursive is called on a node. It loops through this nodes' children, each time calling CallRecursive with each child. CallRecursive calls itself recursively with each of these nodes' children.. When there are no more children it will start to back-track.

Added: Because the MessageBox is placed immediately inside the recursive function, it will produce these messages on the way down the tree, rather than on the way back :)/>

Sorry, it is hard to explain recursion.. especially when tired.. :dozingoff:/>/>/>

You might also search for a procedure that will flatten a TreeView. However, this is likely to be expensive code and may not yield any benefit :)/>/>/>

This post has been edited by andrewsw: 18 January 2013 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3730
  • View blog
  • Posts: 13,025
  • Joined: 12-December 12

Re: What is better for reading

Posted 18 January 2013 - 03:08 PM

Flattening can create a grid (a matrix) representing the tree structure, where you would be able to navigate down a column and/or across rows.

I am not recommending this necessarily, I just mention it as possibility.

Public Function FlattenBreath(ByVal tree As TreeView) As List(Of TreeNode)
        Dim nodes As New List(Of TreeNode)
        Dim queue As New Queue(Of TreeNode)
        Dim top As TreeNode
        Dim nod As TreeNode
        For Each top In tree.Nodes
            queue.Enqueue(top)
        Next
        While (queue.Count > 0)
            top = queue.Dequeue
            nodes.Add(top)
            For Each nod In top.Nodes
                queue.Enqueue(nod)
            Next
        End While
        Return nodes
End Function

[I haven't tested this code.. It doesn't create a grid, just a one-dimensional list.]
http://stackoverflow...treeview-in-net

This post has been edited by andrewsw: 18 January 2013 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3730
  • View blog
  • Posts: 13,025
  • Joined: 12-December 12

Re: What is better for reading

Posted 18 January 2013 - 04:34 PM

Thinking about your specific question, the recursion may not be useful - at least, not as the code stands - if you are comparing two treeviews in parallel. Do the treeviews have exactly the same structure? Added: Looking back at your code it seems you are trying to determine where the two node-structures are not the same.

This post has been edited by andrewsw: 18 January 2013 - 05:27 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3730
  • View blog
  • Posts: 13,025
  • Joined: 12-December 12

Re: What is better for reading

Posted 18 January 2013 - 05:36 PM

I'll post the following code, which uses the FlattenBreadth() (notice the name-correction) function. It is not the same as the OPs requirement unfortunately, but it is interesting none the less, and may provide some inspiration.

It assumes the two treeviews have the same structure and colour-codes differences in the text, using different colours for different levels. [I have no idea whether the flattening is any more efficient than the OPs original code - I suspect it is though.]

        Dim twee As List(Of TreeNode) = FlattenBreadth(TreeView1)
        Dim twee_two As List(Of TreeNode) = FlattenBreadth(TreeView2)

        Dim myTuples As New List(Of Tuple(Of TreeNode, TreeNode))
        If twee.Count() = twee_two.Count() Then
            For x As Integer = 0 To twee.Count - 1
                'creating a list of tuples is not essential..
                myTuples.Add(Tuple.Create(twee(x), twee_two(x)))
                '.. we could just compare twee(x) to twee_two(x)
                If myTuples(x).Item1.Text <> myTuples(x).Item2.Text Then
                    'MsgBox("Found a difference:" & myTuples(x).Item2.Text)
                    Select Case myTuples(x).Item1.Level
                        Case 0
                            myTuples(x).Item1.BackColor = Color.Red
                            myTuples(x).Item2.BackColor = Color.Red
                        Case 1
                            myTuples(x).Item1.BackColor = Color.Green
                            myTuples(x).Item2.BackColor = Color.Green
                        Case Else
                            '
                    End Select
                End If
            Next
            'if you don't want the list of tuples just do 
            For x As Integer = 0 To twee.Count - 1
                If Not twee(x).Text.Equals(twee_two(x).Text) Then
                    twee_two(x).BackColor = Color.Pink  'compare with the previous code
                End If
            Next
        End If


I'll include the renamed function as well:

    Public Function FlattenBreadth(ByVal tree As TreeView) As List(Of TreeNode)
        Dim nodes As New List(Of TreeNode)
        Dim queue As New Queue(Of TreeNode)
        Dim top As TreeNode
        Dim nod As TreeNode
        For Each top In tree.Nodes
            queue.Enqueue(top)
        Next
        While (queue.Count > 0)
            top = queue.Dequeue
            nodes.Add(top)
            For Each nod In top.Nodes
                queue.Enqueue(nod)
            Next
        End While
        Return nodes
    End Function

This post has been edited by andrewsw: 18 January 2013 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1