Load items faster in the TreeView and ListView controls

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:
    XDR solutions

    The Benefits of Using XDR Solutions

    Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

    AI is revolutionizing fraud detection

    How AI is Revolutionizing Fraud Detection

    Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

    AI innovation

    Companies Leading AI Innovation in 2023

    Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

    data fivetran pricing

    Fivetran Pricing Explained

    One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

    kubernetes logging

    Kubernetes Logging: What You Need to Know

    Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

    ransomware cyber attack

    Why Is Ransomware Such a Major Threat?

    One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

    data dictionary

    Tools You Need to Make a Data Dictionary

    Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as