Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Load items faster in the TreeView and ListView controls

Total Hit ( 2596)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


There is an easy, but under-utilized, technique for loading many nodes in a TreeView control (or ListItems in a ListView control) that is faster than the standard technique. Consider this loop:

Click here to copy the following block
For i = 1 To 5000
  TreeView1.Nodes.Add , , , "Node " & i
Next

Instead of repeatedly query the TreeView1 object for its Nodes collection, you can store it in a temporary object variable:

Click here to copy the following block
Dim nods As MSComctlLib.Nodes
Set nods = TreeView1.Nodes
  
For i = 1 To 5000
  nods.Add , , , "Node " & i
Next

You don't even need the temporary variable, if you use a With block:

Click here to copy the following block
With TreeView1.Nodes
  For i = 1 To 5000
    .Add , , , "Node " & i
  Next
End With

On my system, these optimized loops run about 40% faster than the standard code showed above. This faster speed can be explained as follows: by storing the Nodes collection in a temporary variable (or using the hidden temporary variable that VB builds behind the With block), you avoid to bind the Nodes object to its parent TreeView1 object inside the loop. Because this latter binding is based on the inefficient dispid-binding, this step trims a lot of unnecessary overhead inside the loop.
The same reasoning applies to other ActiveX controls:

the ListItems, ListSubItems, and ColumnHeaders collections of the ListView control
the Buttons and ButtonMenus collection of the Toolbar control
the ListImages collection of the ImageList control
the Panels collection of the StatusBar control
the Tabs collection of the TabStrip control
the ComboItems collection of the ImageCombo control the Columns collection of the DataGrid control and more in general, whenever you have an ActiveX control that exposes a collection that you use inside a loop.



Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.