Load items faster in the TreeView and ListView controls

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:

For i = 1 To 5000    TreeView1.Nodes.Add , , , "Node " & iNext

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

Dim nods As MSComctlLib.NodesSet nods = TreeView1.Nodes    For i = 1 To 5000    nods.Add , , , "Node " & iNext

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

With TreeView1.Nodes    For i = 1 To 5000        .Add , , , "Node " & i    NextEnd 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 controland more in general, whenever you have an ActiveX control that exposes a collection that you use inside a loop.

    Share the Post:
    Share on facebook
    Share on twitter
    Share on linkedin

    Overview

    The Latest

    microsoft careers

    Top Careers at Microsoft

    Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

    your company's audio

    4 Areas of Your Company Where Your Audio Really Matters

    Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

    chrome os developer mode

    How to Turn on Chrome OS Developer Mode

    Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS