Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Adding nodes to a treeview control gets progressively slower

Posted on 2008-06-25
Medium Priority
Last Modified: 2013-12-25
Hi, first time asking a question here - so be gentle with me! I've been searching solutions for this problem, but couldn't really find anything that fit.

I have a vb6 project wich is fairly complex. In it I have a treeview, which lists quite a few items (the subnodes are not added before they are viewed). The treeview takes quite long to build, and it seems it gets progressively slower with a larger amount of nodes.

An itemset with 3570 nodes takes 41,5 seconds, 4369 takes 52,7 seconds - and a set combining both takes 119 seconds (just adding the time it takes for set 1 and 2 gives around 94 seconds). Should I expect it to be like this?

The nodes are added in a while loop going through the dataset, adding them like this:
TreeView1.Nodes.Add(, , key, label)

Before the loop, the tree is set to not visible:
TreeView1.Visible = False
to help on the time it takes to set it up. It helps (examples shown are with the tree already set to not visible during building it).

Any ideas what I can do/look at to get this thing to go faster?
Question by:VegInformatikk
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
  • 2
  • 2
LVL 22

Expert Comment

ID: 21866150
In lieu of TreeView1.Visible = False try this:

BeforeUpdate TreeView1.hWnd
'Load nodes
Private Declare Function LockWindowUpdate Lib "user32.dll" (ByVal hwndLock As Long) As Long
Public Sub BeginUpdate(ByVal hWnd As Long)
   LockWindowUpdate hWnd
End Sub
Public Sub EndUpdate()
   LockWindowUpdate 0
End Sub

Open in new window


Author Comment

ID: 21872250
Tried replacing .visible=false with LockWindowUpdate, but the tree was built in the same timespan as the tries I had with .visible = false. I tried it without .visible = false and without using LockWindowUpdate just to make sure I had the correct handle - and I definately had the right one.

Thanks for the attempt though!
LVL 17

Accepted Solution

zzzzzooc earned 2000 total points
ID: 21888162
Are you using DoEvents? If you are, you should either remove it or only use it every 1000/items as it will dramatically slow down your loop.

For me, adding 3000 nodes without DoEvents took 0 seconds -- with DoEvents, 40+ seconds.

Author Comment

ID: 21897541
I had a DoEvents hidden away in my main addnode loop, and I tried just removing that. It really helped, though it's still a bit slow. At around 11000 nodes (my biggest test set) it went from 3 minutes and 20 seconds, to 26 seconds. I've started working on a grid 'in front' of my treeview, to greatly reduce the size of the dataset to be opened in the treeview. That coupled with the removal of the DoEvents should make for a much smoother ride.
LVL 17

Expert Comment

ID: 21902681
Glad it worked.

Just remember, without DoEvents, your window won't process it's internal messages (painting, etc) so you don't need to worry about hiding your listview or using similar methods. It *will* appear to be "frozen" so every 5-10/secs you might want to use a DoEvents but it will of course slow the procedure down but at least users won't think it's frozen.

My only other suggestions would be to remove any other unnecessary processing in your loop and if working with strings, uses the corresponding string functions (such as Mid$() instead of Mid()) as one will return a String whereas the other a Variant in which case type-casting is unnecessarily done. Large amounts of these conversions would show a performance-hit.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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

705 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