J
JMANCI
Guest
I'm converting a VB6 Treeview to the .NET Treeview. I re-did the code so that it loads the Treeview recursively and the speed is great - same load speed as it was in VB6. But I'm having an issue with the speed when I try to number the treeview with letters/numbers. While the numbering/lettering is correct, it's very slow to run. I noticed it gets slower as the numbering code moves it's way down the tree but not sure why. I tried to throw in a Treeview.BeginUpdate and a Treeview.EndUpdate in parent/child code, but that didn't speed things up. I have a total of just over 1,000 nodes so I think it's reasonable to load the tree this way, rather than doing a dynamic Tree load. Here's what I have:
I start with this code, then get each Parent.
Private Sub RecursiveNumbering(ByVal mytreeView As TreeView)
' Print each node recursively.
Dim nodes As TreeNodeCollection = mytreeView.Nodes
For Each n As TreeNode In nodes
'Number the Parent
Call RecursiveNumberParent
'Reset our child number array
For iChild = 1 To 5
LevChildCount(iChild) = 1
Next iChild
'Now loop through the children
PrintRecursiveChild
Next
End Sub
Private Sub RecursiveNumberParent(ByRef MyNode As TreeNode)
'numbers all nodes on the tree. If inumbercode = 1, begin numbering
'w/ cap letters. Otherwise, begin numbering with arabic number.
ThisLev = MyLevel(MyNode.FullPath)
'If ThisLev < PrevLev Then
'we've started a new level up, so need to reset all lower levels
For iParent = ThisLev + 1 To 5
LevCount(iParent) = 1
Next iParent
'End If
trvtest0.BeginUpdate()
MyNode.Text = myNumber(ThisLev, LevCount(ThisLev)) & MyNode.Text
trvtest0.EndUpdate()
LevCount(ThisLev) = LevCount(ThisLev) + 1
PrevLev = ThisLev
End Sub
The Child Code
Private Sub PrintRecursiveChild(ByVal n As TreeNode)
Dim aNode As New TreeNode
Try
For Each aNode In n.Nodes
Dim ErrMsg As String
ThisChildLev = MyLevel(aNode.FullPath)
If ThisChildLev < PrevChildLev Then
'we've started a new level up, so need to reset all lower levels
For iChild = ThisChildLev + 1 To 5
LevChildCount(iChild) = 1
Next iChild
End If
'threw this in here as I was getting an Out of Memory error. This fixed it.
Application.DoEvents()
aNode.Text = myNumber(ThisChildLev, LevChildCount(ThisChildLev)) & aNode.Text
LevChildCount(ThisChildLev) = LevChildCount(ThisChildLev) + 1
PrevChildLev = ThisChildLev
'Get next child
PrintRecursiveChild(aNode)
Next
Catch ex As Exception
Call MsgBox(Err.Description)
End Try
End Sub
Code to get the Level of the Node
Private Function MyLevel(ByVal sPath As String) As Short
'function returns level of passed path of a node
Dim ErrMsg, StrHolder As String
Dim iPos As Integer
Try
MyLevel = 0
Do While InStr(sPath, "\") <> 0
StrHolder = sPath
iPos = InStr(StrHolder, "\")
sPath = Mid(StrHolder, iPos + 1, Len(StrHolder) - iPos)
MyLevel = MyLevel + 1
Loop
Catch ex As Exception
ErrMsg = ErrString & "Error # " & Str(Err.Number) & " was generated by MyLevel_Err: " & Err.Source & Chr(13) & Err.Description
Call LogErr(Str(Err.Number), Err.Description, "MyLevel_Err")
MsgBox(ErrMsg, , "Error")
End Try
End Function
Continue reading...
I start with this code, then get each Parent.
Private Sub RecursiveNumbering(ByVal mytreeView As TreeView)
' Print each node recursively.
Dim nodes As TreeNodeCollection = mytreeView.Nodes
For Each n As TreeNode In nodes
'Number the Parent
Call RecursiveNumberParent
'Reset our child number array
For iChild = 1 To 5
LevChildCount(iChild) = 1
Next iChild
'Now loop through the children
PrintRecursiveChild
Next
End Sub
Private Sub RecursiveNumberParent(ByRef MyNode As TreeNode)
'numbers all nodes on the tree. If inumbercode = 1, begin numbering
'w/ cap letters. Otherwise, begin numbering with arabic number.
ThisLev = MyLevel(MyNode.FullPath)
'If ThisLev < PrevLev Then
'we've started a new level up, so need to reset all lower levels
For iParent = ThisLev + 1 To 5
LevCount(iParent) = 1
Next iParent
'End If
trvtest0.BeginUpdate()
MyNode.Text = myNumber(ThisLev, LevCount(ThisLev)) & MyNode.Text
trvtest0.EndUpdate()
LevCount(ThisLev) = LevCount(ThisLev) + 1
PrevLev = ThisLev
End Sub
The Child Code
Private Sub PrintRecursiveChild(ByVal n As TreeNode)
Dim aNode As New TreeNode
Try
For Each aNode In n.Nodes
Dim ErrMsg As String
ThisChildLev = MyLevel(aNode.FullPath)
If ThisChildLev < PrevChildLev Then
'we've started a new level up, so need to reset all lower levels
For iChild = ThisChildLev + 1 To 5
LevChildCount(iChild) = 1
Next iChild
End If
'threw this in here as I was getting an Out of Memory error. This fixed it.
Application.DoEvents()
aNode.Text = myNumber(ThisChildLev, LevChildCount(ThisChildLev)) & aNode.Text
LevChildCount(ThisChildLev) = LevChildCount(ThisChildLev) + 1
PrevChildLev = ThisChildLev
'Get next child
PrintRecursiveChild(aNode)
Next
Catch ex As Exception
Call MsgBox(Err.Description)
End Try
End Sub
Code to get the Level of the Node
Private Function MyLevel(ByVal sPath As String) As Short
'function returns level of passed path of a node
Dim ErrMsg, StrHolder As String
Dim iPos As Integer
Try
MyLevel = 0
Do While InStr(sPath, "\") <> 0
StrHolder = sPath
iPos = InStr(StrHolder, "\")
sPath = Mid(StrHolder, iPos + 1, Len(StrHolder) - iPos)
MyLevel = MyLevel + 1
Loop
Catch ex As Exception
ErrMsg = ErrString & "Error # " & Str(Err.Number) & " was generated by MyLevel_Err: " & Err.Source & Chr(13) & Err.Description
Call LogErr(Str(Err.Number), Err.Description, "MyLevel_Err")
MsgBox(ErrMsg, , "Error")
End Try
End Function
Continue reading...