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

Posted on 2003-03-04
Medium Priority
Last Modified: 2010-05-01

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)
   ' End If
end sub

Thank You
Question by:MariuszK
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
LVL 28

Accepted Solution

AzraSound earned 100 total points
ID: 8066642
You may consider loading the child nodes dynamically when the parent is expanded.   When the parent is being expanded, determine if it has any children, and if not, perform your search to grab the children and add them to the treeview.

Expert Comment

ID: 8068320
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.
LVL 49

Expert Comment

ID: 9046781
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 9073613
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.


LVL 28

Expert Comment

ID: 9073700
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.

Expert Comment

ID: 9074550
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)



LVL 49

Expert Comment

ID: 9074901
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
LVL 28

Expert Comment

ID: 9080382

Expert Comment

ID: 9102972

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question