Link to home
Start Free TrialLog in
Avatar of MariuszK
MariuszK

asked on

How can I optimize TreeView to get the most efficent speed?

Hello,

I have a situation where I need to insert a large number of nodes into a treeview control.  Well, first of all I'm not sure what are the limitations of treeview are. Is the indexing same as with Listbox (32,767) ?

And how could I insert 10,000 or maybe 1,000,000 nodes in the most speed efficient maner?

Here is the code I'm using now:

'*******************************************************************
Friend Sub GetAccounts(trvTemp As TreeView, nodTemp As Node, _
  Optional bRefresh As Boolean, Optional pbTemp As ProgressBar, Optional lblUsers As Label)
'*******************************************************************
  Dim rsTemp  As Recordset, rsTemp2 As Recordset                    ' Declare Local Objects
  Dim strRel1 As String, strRel2 As String, strRel3 As String
  Dim strKey  As String, strTag  As String
  Dim strText As String, strFirstSibl As String, intImage As Integer
  Dim pbFlag  As Boolean                                            ' Determins Existance Of ProgressBar Parm
  Dim Node    As Node
Do While Not rsTemp.EOF
  If pbFlag Then
      If pbTemp.Value Mod pbTemp.Max * 0.1 = 0 Then DoEvents        ' Let CPU Do Some Stuff
      pbTemp.Value = pbTemp.Value + 1                               ' If Progress Active, Update It
    End If
     
    Set Node = tree.Nodes.Add(nodTemp.Key, tvwChild, nodTemp.Key & rsTemp.fields("Name"), rsTemp.fields("Name"), BMP_USERS)
    trvTemp.Nodes(Node.Index).Tag = rsTemp!Id

      On Error Resume Next
      rsTemp2.Filter = "Id ='" & rsTemp2.fields("Id") & "'"
      rsTemp2.MoveFirst                                               ' Move To First Record
      On Error GoTo 0
      Do While Not (rsTemp2.EOF)
        Set Node = trvTemp.Nodes.Add(nodTemp.Key & rsTemp.fields("Name"), tvwChild, nodTemp.Key & rsTemp.fields("Name") & rsTemp2.fields("Name"), rsTemp2.fields("Name"), BMP_RTUSR)
        rsTemp2.MoveNext
      Loop
   ' End If
    rsTemp.MoveNext
  Loop
end sub

Thank You
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PhiliP
PhiliP

try to rethink your solution. Consider: who is going to search in you treeview of 1 miljon nodes to find something. I woudn't. Even more than 1000 is to many.

In cases where I had this problem I start out by giving users a number of search possibilities. This way i narrow down the number of results to process.

Start asking questions to your users indicating how they like to get to their data.
Avatar of DanRollins
Hi MariuszK,
It appears that you have forgotten to close this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept AzraSound's comment(s) as an answer.
    *** PhiliP is right too, but too few points to split

MariuszK, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
I disagree,
The question is about speeding up filling a treeview with countless nodes. Although The answer of AzraSound looks good. In no way will the treeview perform. Even a out of memory can be the case when using a treeview like MariuszK and AzraSound propose.

So the rethink your solution is in my opinion the more logical answer.

And offcause Azra has earned enough points lately.

HaveLotsOfFun

PhiliP
1. Of course my solution improves performance.  The reason there is an intial performance hit is because you have to sit there and wait for tens of thousands of nodes to be populated in the treeview (even though a great majority of them wont even be visible).  With my method, you load only the parents, and dynamically retrieve its children when that node is expanded.  It means a bit slower load of the children at that point, but makes the overall experience more efficient.

2. Out of memory will occur if the user expands enough nodes to see whatever the limit is on the resources.  This would go even against your theory since obviously the user would have been willing to travel through and search through that many items.

3. This whole "AzraSound already has too many points" argument is getting old.  This has nothing to do with you PhiliP, but you aren't the first to mention it.  There is a reason I have a lot of points...because I put in the time and energy to answer a lot of questions.  There is no reason to penalize me because of that.  We don't take the "Robin Hood" approach with points at EE.
OK. I give up.

The only adition to AzraSounds solution would be: Keep only the active node in memory (Unload the previous visible children and collapse the 'old node'. That would offcause hamper any drag and drop but it's a trade off.

Give the Points to AzraSound. (I didn't provide a solution, he did)

And to be honnest I didn't in any way try to hurt anybody's feelings on purpose. Honor to those who have to be honored. (I'm not a native english speaker, remember)

HaveTonsOfFun

Philip



I agree that AzraSound has too many points.  He just passed me in the HOF.  AzraSound, please stop providing so many excellent answers so I can catch up.  Thanks!

:-) Dan
LMAO!
Wat???????????????????