Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

580 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