Mega Code Archive

 
Categories / Delphi / VCL
 

Optimization of work with standard TTreeViewTListView components

Title: Optimization of work with standard TTreeView/TListView components Question: Have a bad performance with standard TTreeView/TListView? Read this tip and implement the hints... Answer: If you uses the TTreeView and/or TListView from Win32 page of default component palette, then you must know that if you have the large amount nodes, you have a very bad performance... Of course, at same moment you'll try to find a some other third-party component that allow to work with your very large data but I want to give you the few hints which allows to increase a performance without any third-party components. Only using of optimized code. Tip1: if you need add a lot of nodes in same time (for example, after button click to load the 10000 nodes in tree from some source) then you must call: yourTreeView.Items.BeginUpdate; yourTreeView.Items.EndUpdate; This constuction will disable a repainting when you append the nodes - it's save a lot of time! Tip2: if you uses the some navigation by nodes, you must use the GetFirst and GetNext methods instead Items[i] using! For example, var node: TTreeNode; begin node := yourTreeView.Items.GetFirstNode; repeat node := Result.GetNext; until node = nil; end; It's save a lot of time too! The GetFirstNode/GetNext is faster than standard for i := 0 to yourTreeView.Items.Count-1 do begin node := yourTreeView.Items[i]; end; For example, in own warehouse system I have a treeview with 5000 nodes which I load from Oracle resultset. After applying of these tips, the time of execution of procedure with append was decreased from 4-5 minutes to 15-20 seconds! Trust me:-) I don't sure but I think that it's a bad work of Borland when team developed the envelope for Win's treeview/listview. But maybe I'm wrong. PS: of course, if you have a very-very large data with few billions of nodes or after applying of tips above all the same you have a bad performance, you must use the virtual mode of control or really select the other third-party control. But I suggest to change the your inteface logic - to navigate thru few billions of nodes in same time is very hard task for user! Not only for you:-) With best regards, Mike Shkolnik. E-Mail: mshkolnik@scalabium.com mshkolnik@yahoo.com WEB: http://www.scalabium.com SMComponents Mailing list: http://www.onelist.com/subscribe/SMComponents