Quickly transfer to TreeView controls and child contents of the ListView control

xiaoxiao2021-03-06  95

There is a simple but still unrecognized tip that can be used to load multiple nodes in the TreeView control, or load multiple ListItems in the ListView control. This approach is faster than traditional practices. Let's take a look at this traditional method:

For i = 1 to 5000

TreeView1.nodes.add,, "Node" & I

NEXT

Improvement, instead of referencing the NODES collection of TreeView1 objects, we can save it in the temporary object variable:

DIM NODS as Mscomctllib.nodes

Set nods = TreeView1.nodes

For i = 1 to 5000

Nods.add,, "Node" & I

NEXT

Even if you use the WITH code block, you can also do not need temporary variables:

With treeciew1.nodes

For i = 1 to 5000

.Add,, "node" & i

NEXT

End with

Tested, the optimized cycle code is about 40% faster than the traditional method. The reason is that the NODES collection object is saved in the temporary variable, or the VB after applying the WITH code block will use the hidden temporary variable to avoid repeatedly bind Nodes objects to its parent TreeView1 object in the loop. Since this binding is inefficient, it can save a lot of execution time.

The same reason is also effective for other ActiveX controls:

ListItems, ListSubitems, and ColumnHeaders Collection Toolbar Controls Buttons and ButtonMenus Collection Imagelist's Listimages Collection Statusbar Control PANS Collection Tabstrip Control Tabs Collection

转载请注明原文地址:https://www.9cbs.com/read-96901.html

New Post(0)